2016-11-12 77 views
0

我在视图的下半部分有一个textfield,所以我使用下面的函数来移动我的视图。 但实际上一切都看起来像我想成为除键盘上的黑色区域。如何处理它?任何帮助表示赞赏。 here's the screen移动键盘时的黑色区域

override func viewDidLoad() { 

     NotificationCenter.default.addObserver(self, selector: #selector(ViewControllerForTextfield.keyboardWillShow(_:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil); 
     NotificationCenter.default.addObserver(self, selector: #selector(ViewControllerForTextfield.keyboardWillHide(_:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil); 
     textField.delegate = self 
     super.viewDidLoad() 
    } 


func keyboardWillShow(_ sender: Notification) { 
    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
     if self.view.frame.origin.y == 0{ 
      self.view.frame.origin.y -= keyboardSize.height + 100 
     } 
    } 

} 

func keyboardWillHide(_ sender: Notification) { 
    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { 
     if self.view.frame.origin.y != 0{ 
      self.view.frame.origin.y += keyboardSize.height 
     } 
    } 
} 

}

回答

0

我相信310是太多的拉升。就自定义键盘而言,确定键盘的高度并不容易。但是,this post解释了如何获得键盘的高度。

编辑: 也许你还想要从文本字段到屏幕底部的距离的差异?

+0

我同意310这是太多,但我有一个ImageView的顶上的文本框的,所以我不想裁剪。感谢你,虽然 – tyg1

+0

一点点改变了我的代码 – tyg1

+0

@ tyg1我编辑我的答案的答案。也许它的键盘+ TextField + ImageView不适合屏幕。如果是这种情况,您需要更改ImageView的大小或裁剪它。 但是,如果你觉得它的话,请将答案标记为正确。 –