2016-03-22 26 views
0

我有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) 
    } 
} 

有關它的工作原理看thisthis回答更多信息。

當您運行鍵盤它可能會很好地工作 - 這是略高與旋轉後的橙色視圖覆蓋整個鍵盤:

enter image description here

然而,你也可能得到這個(縱向高度旋轉後保持):

enter image description here

步驟來重現isue(與上面的代碼):

  1. 打開鍵盤消息應用程序中,旋轉景觀和背部
  2. 如果您沒有看到這個問題殺消息應用程序(按Home鍵2倍 - >刷卡的消息了)
  3. 開放消息再次,旋轉景觀和背部
  4. 您可能需要重複步驟1-3幾次看問題(通常不超過2-4次)

我知道什麼:

  • 如果你看到的問題,您會繼續seing,直到鍵盤被隱藏和重新顯示
  • 如果你看到問題隱藏和重新顯示鍵盤withing相同的應用程序 - 這個問題總是在前看不見
  • 問題可能只殺死並重新啓動託管應用程序
  • Debuger表明heightConstraint!.constant193後再次出現,但兩者view.frame.heightview.window?.frame.height仍然253(改變幀直接不能解決問題)

我曾嘗試:

  • 而不是隻需設置約束heightConstraint!.constant = desiredHeight首先從view刪除,設置新的值,然後重新添加
  • 我驗證了heightConstraint!.constant總是改變當它應該是

如何解決或解決此問題?必須有一個解決方案,因爲SwiftKey沒有這個問題。

回答

1

我遇到類似的問題與鍵盤擴展 - 在iPhone 6+機型,它總是未能正確設置它的高度上旋轉的第一一次鍵盤在一個應用程序調用,偶爾上iPhone 6和其他iPhone機型。

我終於找到了一個解決方案,雖然它有它自己的缺點。

首先,文檔指出

在iOS中8.0,你可以調整自定義鍵盤的高度後的任何時間其主要觀點最初繪製在屏幕上。

您正在設置高度限制-[UIViewController viewWillAppear:],根據對文檔的嚴格解釋,這太早了。如果您將其設置爲-[UIViewController viewDidAppear:],則應解決您當前遇到的問題。

但是,您可能會發現,您的高度調整是視覺上的震撼,因爲它出現在鍵盤出現之後。

我不知道如何SwiftKey設置其高度(似乎)之前,鍵盤出現避免此問題。

+0

謝謝!看起來像在'viewDidAppear'中添加約束來解決這個問題。至於導致視覺刺激身高的問題:基於我在iOS 9上的測試,應用程序中的第一次出現總是OK,無論在viewWillAppear或viewDidAppear中是否添加了約束,任何後續事件都會產生衝突。在iOS 8上有差異,但不是那麼大......你有不同的經驗嗎? – drasto