2016-03-01 71 views
0

我使用iOS9與swift2.0.I我的視圖控制器處理兩個文本框和一些labels.This 2個文本框包含stackview和標籤還包含另一stackview.I已經建立通用的應用程序都縱向和橫向選項,其工作正常。但是當我點擊textfields然後鍵盤是在那個時候鍵盤覆蓋textfields,所以我看不到textfield data.How解決這個問題,以及如何管理所有約束時,鍵盤是和下降?UIStackview與UIKeyboard在swift2.0

override func viewWillAppear(animated: Bool) { 

    super.viewWillAppear(animated) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector:("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object:nil) 
    NSNotificationCenter.defaultCenter().addObserver(self, selector:("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil) 

} 
override func viewWillDisappear(animated: Bool) { 

    super.viewWillDisappear(animated) 
    NSNotificationCenter.defaultCenter().removeObserver(self) 
} 

func keyboardWillShow(notification:NSNotification){ 

    print("keyboardWillShow") 
    var info = notification.userInfo! 
    let keyboardFrame:CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 
    print(keyboardFrame) 
    UIView.animateWithDuration(0.1, animations: {() -> Void in 

    }) 
} 

func keyboardWillHide(notification:NSNotification){ 

     print("keyboardWillHide") 

} 

enter image description here

enter image description here

回答

2
func keyboardWillShow(sender: NSNotification){ 

    var info = sender.userInfo! 
    let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 


    UIView.animateWithDuration(0.1, animations: {() -> Void in 

     self.view.center.y = self.view.center.y - keyboardFrame.size.height 
    }) 


} 
func keyboardWillHide(sender: NSNotification){ 

    var info = sender.userInfo! 
    let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue() 

    UIView.animateWithDuration(0.1, animations: {() -> Void in 
     self.view.center.y = self.view.center.y + keyboardFrame.size.height 
    }) 

} 
+0

它的點擊都textfields.Its時不能正常工作移動視圖了。 – IKKA

+0

你想要什麼? @jasper它將你的視圖移動到上面的鍵盤上,如果你只需要textfield移動不是整個視圖,你可以設置你的文本框中心而不是像textfield.center.y這樣的視圖,也可以通過標記來檢測你的文本框。 –