2016-11-22 25 views
0

這裏是我的代碼,鍵盤出現時將一個視圖切換了(注意,這個觀點有一個CollectionView):斯威夫特的iOS 9鍵盤出現不移位觀看,但工作在iOS的10

if isKeyboardShowing { 
    UIView.animate(withDuration: animationDuration, animations: { 
     if self.isCollectionViewBlockingInput() { 
      self.view.frame.origin.y = -distanceShifted 
      // Keyboard is shifted less because view already shifts by distance 
      self.bottomConstraintForInput.constant = -keyboardViewEndFrame.height + distanceShifted 
     } else { 
      self.bottomConstraintForInput.constant = -keyboardViewEndFrame.height 
     } 
    }) 
} 

本質視圖的原點y向上移動一定距離。這適用於iOS 10,但在iOS 9 **中,當視圖第一次加載並且我第一次打開鍵盤時,視圖會嘗試向上移動但失敗。相反,一個黑色的背景似乎暫時下移,然後消失:

Black part that appears momentarily then disappears in iOS 9 這只是發生在滾動集合視圖之前打開鍵盤。一旦我開始滾動收藏視圖,錯誤消失,鍵盤再次正確移動。

此外,此錯誤並未在iOS 10中顯示。

+0

你爲什麼不叫UIView'的'了'layoutIfNeeded'方法,並使用完畢塊動畫和編寫裏面。 –

回答

0

試試這個

UIView.animate(withDuration: 1.0, animations: { 
    //Do your constraint change part here 

}, completion: {bool in 

     // Call here the layoutIfNeeded method 
     self.view.layoutIfNeeded() 
}) 
+0

試過^它不工作。 – leonardloo

+0

我意識到它在我翻轉訂單並首先執行self.view.layoutIfNeeded()時工作,然後在完成時更新我的​​約束。但是,由於視圖向上移動,因此顯示黑色背景。我如何使背景背後的背景變成白色? – leonardloo