I am displaying a view in the bottom of the universal application and adding this view dynamically in my view. I want to show this view in bottom every time like iAd. in bot
Extending @Alex Shubin solution in Swift 4, I do the following:
Sometimes, I need to add a variable number of constraints, in this case 3, because the height will be calculated latter.
keyboard.addConstaints(top: nil, right: 0.0, bottom: 0.0, left: 0.0, width: nil, height: nil)
You have to be careful adding all required, and making sure you don't have any conflicting constraints.
extension UIView {
func addConstaints(top: CGFloat?, right: CGFloat?, bottom: CGFloat?, left: CGFloat?, width: CGFloat?, height: CGFloat?) {
translatesAutoresizingMaskIntoConstraints = false
if top != nil { self.addConstaint(top: top!) }
if right != nil { self.addConstaint(right: right!) }
// Add lines for bottom, left, width an heigh
// ...
}
func addConstaint(top offset: CGFloat) {
guard superview != nil else { return }
topAnchor.constraint(equalTo: superview!.topAnchor, constant: offset).isActive = true
}
}
To verify that all views were laid-out correctly, you can inspect the .constraints property of the superView or you can check the frame in:
override func viewDidLayoutSubviews() {
print(keyboard.frame)
}