В IOS 10 (быстрый 4) мне пришлось объединить приведенные выше ответы по трем причинам:
updateViewConstraints
не вызывается при повороте iPhone heightConstraint
создает ограничение, которое игнорируется макетом intrinsicContentSize
, выполнялось только при обстоятельствах, которые я не понял @objc public class CustomInputView: UIInputView {
var intrinsicHeight: CGFloat = 296.0 {
didSet {
self.invalidateIntrinsicContentSize()
}
}
@objc public init() {
super.init(frame: CGRect(), inputViewStyle: .keyboard)
self.translatesAutoresizingMaskIntoConstraints = false
}
@objc public required init?(coder: NSCoder) {
super.init(coder: coder)
self.translatesAutoresizingMaskIntoConstraints = false
}
@objc public override var intrinsicContentSize: CGSize {
let screenSize = UIScreen.main.bounds.size
let newHeight :CGFloat = screenSize.width > screenSize.height ? 230.0 : intrinsicHeight
return CGSize(width: UIViewNoIntrinsicMetric, height: newHeight)
}
}
@objc public class KeyboardViewController: UIInputViewController {
let portraitHeight:CGFloat = 296.0
let landscapeHeight:CGFloat = 230.0
var heightConstraint: NSLayoutConstraint?
func updateHeightConstraint(to size: CGSize){
var heightConstant=portraitHeight
if size.width>400 {
heightConstant=landscapeHeight
}
if heightConstant != heightConstraint!.constant {
inputView?.removeConstraint(heightConstraint!)
heightConstraint!.constant = heightConstant;
inputView?.addConstraint(heightConstraint!)
}
}
override public func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
updateHeightConstraint(to: size)
}
override public func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
updateHeightConstraint(to: UIScreen.main.bounds.size)
}
override public func viewDidLoad() {
super.viewDidLoad()
heightConstraint = NSLayoutConstraint(item: self.inputView as Any, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: portraitHeight)
heightConstraint!.priority = UILayoutPriority(rawValue: 999.0)
heightConstraint!.isActive=true;
}
//... code to insert, delete,..
}
в viewDidAppear
Мне пришлось называть updateHeightConstraint
, потому что viewWillTransition
не вызывался, когда я менял UIInputViewController
Мне не нужно self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false