简体   繁体   English

Swift 4:当键盘显示时移动 uiview

[英]Swift 4 : moving uiview when keyboard shows

I have a Uiviewcontroller, in this viewcontroller I put a uiview called categUIV.我有一个 Uiviewcontroller,在这个 viewcontroller 中我放了一个名为 categUIV 的 uiview。 The categUIV contains uitexfields and a button. categUIV 包含 uitexfields 和一个按钮。 The idea is to move the hole categUIV up when the keyboard is up.这个想法是在键盘抬起时向上移动 categUIV 孔。

I use the code below我使用下面的代码

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)            
}   

func keyboardWillShow(notification: NSNotification) {            
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
    self.categUIV.frame.origin.y -= keyboardSize.height
}            
}

 func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
    self.categUIV.frame.origin.y += keyboardSize.height
}
 }

Now when I click to a uitextfied the keyboard pops up and categUIV pops up correctly, the problem is when I start typing the categUIV go back to his initial position by itself.现在,当我单击 uitextfied 时,键盘会弹出并且 categUIV 正确弹出,问题是当我开始键入时,categUIV 会自行返回到他的初始位置。

I don't know why but i wonder is that maybe because of the constraints of categUIV?我不知道为什么,但我想知道这可能是因为 categUIV 的限制吗?

The problem is with frame-layout things don't work as expected , Try autolayout , hook the bottom constraint of the categUIV as IBOutlet and do this问题是框​​架布局的东西不能按预期工作,尝试自动布局,将 categUIV 的底部约束挂钩为 IBOutlet 并执行此操作

 @objc  func handleKeyboardDidShow (notification: NSNotification)
 {
    let keyboardRectAsObject = notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue

    var keyboardRect = CGRect.zero

    keyboardRectAsObject.getValue(&keyboardRect)

    self.categUIVBotcon.constant = -1 * keyboardRect.height 

    UIView.animate(withDuration: 0.5,animations: {

       self.view.layoutIfNeeded()

    })

} 

@objc func handleKeyboardWillHide(notification: NSNotification)
{

    self.categUIVBotcon.constant = 0

    UIView.animate(withDuration: 0.5,animations: {

       self.view.layoutIfNeeded()

    })

}

To make this work, you need to put your view into a scroll view so that your hierarchy looks like this:要完成这项工作,您需要将您的视图放入滚动视图中,以便您的层次结构如下所示:

- View Controller
  - View
    - Scroll View
      - Content View
        - categUIV 

You must also connect the scroll view and the categUIV to your code:您还必须将滚动视图和 categUIV 连接到您的代码:

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var categUIV: UIView!

You can then register your keyboard observers:然后你可以注册你的键盘观察者:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

With the following functions:具有以下功能:

func keyboardWasShown(notification: NSNotification) {
    self.keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
    self.scrollView.isScrollEnabled = true
    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, self.keyboardSize.height, 0.0)
    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
    var rect = self.view.frame
    rect.size.height -= self.keyboardSize.height
    if !rect.contains(self.categUIV.frame.origin) {
        self.scrollView.scrollRectToVisible(self.categUIV.frame, animated: true)
    }
}

func keyboardWillBeHidden(notification: NSNotification) {
    let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize.height, 0.0)
    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
    self.scrollView.isScrollEnabled = false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM