简体   繁体   中英

Swift 4 : moving uiview when keyboard shows

I have a Uiviewcontroller, in this viewcontroller I put a uiview called categUIV. The categUIV contains uitexfields and a button. The idea is to move the hole categUIV up when the keyboard is up.

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.

I don't know why but i wonder is that maybe because of the constraints of 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

 @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:

@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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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