我有iOS自定義鍵盤在旋轉時改變高度。旋轉後不正確的鍵盤擴展高度
我的代碼可以正常工作95%...但在某些情況下(見下文),當旋轉到橫向時高度不會改變 - 縱向高度保持不變。
發行可以用這個(幾乎)最少的代碼進行復制 - 創建新的鍵盤擴展目標,並將此代碼複製到KeyboardViewController
:
class KeyboardViewController: UIInputViewController {
private var orangeView = UIView()
private var heightConstraint: NSLayoutConstraint!
@IBOutlet var nextKeyboardButton: UIButton!
override func updateViewConstraints() {
super.updateViewConstraints()
let screenSize = UIScreen.mainScreen().bounds.size
let screenH = screenSize.height
let screenW = screenSize.width
let isLandscape = !(self.view.frame.size.width == screenW * ((screenW < screenH) ? 1 : 0) + screenH * ((screenW > screenH) ? 1 : 0))
let desiredHeight: CGFloat = isLandscape ? 193 : 252
if heightConstraint.constant != desiredHeight {
heightConstraint!.constant = desiredHeight
orangeView.frame = CGRect(x: 0, y: 0, width: screenW, height: isLandscape ? 193 : 252)
}
}
override func viewDidLoad() {
super.viewDidLoad()
nextKeyboardButton = UIButton(type: .System)
nextKeyboardButton.setTitle("Next Keyboard", forState: .Normal)
nextKeyboardButton.sizeToFit()
nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
heightConstraint = NSLayoutConstraint(item:self.inputView!, attribute:.Height, relatedBy:.Equal, toItem:nil, attribute:.NotAnAttribute, multiplier: 0.0, constant: 0) //preparing heightConstraint
heightConstraint?.priority = 999
orangeView.backgroundColor = UIColor.orangeColor()
view.addSubview(orangeView)
view.addSubview(self.nextKeyboardButton)
let nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
let nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
view.addConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint])
}
override func viewWillAppear(animated: Bool) {
if self.view.constraints.filter({c in c == self.heightConstraint}).isEmpty {
self.view.addConstraint(heightConstraint)
}
view.setNeedsUpdateConstraints() //ensures that updateViewConstraints always gets called at least once
super.viewWillAppear(animated)
}
}
當您運行鍵盤它可能會很好地工作 - 這是略高與旋轉後的橙色視圖覆蓋整個鍵盤:
然而,你也可能得到這個(縱向高度旋轉後保持):
步驟來重現isue(與上面的代碼):
- 打開鍵盤消息應用程序中,旋轉景觀和背部
- 如果您沒有看到這個問題殺消息應用程序(按Home鍵2倍 - >刷卡的消息了)
- 開放消息再次,旋轉景觀和背部
- 您可能需要重複步驟1-3幾次看問題(通常不超過2-4次)
我知道什麼:
- 如果你看到的問題,您會繼續seing,直到鍵盤被隱藏和重新顯示
- 如果你看到問題隱藏和重新顯示鍵盤withing相同的應用程序 - 這個問題總是在前看不見
- 問題可能只殺死並重新啓動託管應用程序
- Debuger表明
heightConstraint!.constant
是193
後再次出現,但兩者view.frame.height
和view.window?.frame.height
仍然253
(改變幀直接不能解決問題)
我曾嘗試:
- 而不是隻需設置約束
heightConstraint!.constant = desiredHeight
首先從view
刪除,設置新的值,然後重新添加 - 我驗證了
heightConstraint!.constant
總是改變當它應該是
如何解決或解決此問題?必須有一個解決方案,因爲SwiftKey沒有這個問題。
謝謝!看起來像在'viewDidAppear'中添加約束來解決這個問題。至於導致視覺刺激身高的問題:基於我在iOS 9上的測試,應用程序中的第一次出現總是OK,無論在viewWillAppear或viewDidAppear中是否添加了約束,任何後續事件都會產生衝突。在iOS 8上有差異,但不是那麼大......你有不同的經驗嗎? – drasto