KeyboardWillShow only moves container UIView for certain UITextFields

风格不统一 提交于 2019-12-11 08:49:10

问题


I have a container UIView that contains a UICollectionView and a UIView, both of which have UITextFields in them. I used NSNotificationCenter to move the container view when the keyboard pops up.

However, it only seems to work for the UICollectionView but not the sub UIView, as shown in the screenshots:

works fine for the text fields in UICollectionView:

but doesn't work for the text field in the UIView:

EDIT: related code:

override func viewWillAppear(animated: Bool) {

    super.viewWillAppear(animated)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)

}



override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardSize =  (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            kbHeight = keyboardSize.height
            self.animateDurationView(true)
        }
    }
}

func keyboardWillHide(notification: NSNotification) {
    self.animateDurationView(false)
}

func animateDurationView(up: Bool) {
    var movement = (up ? -kbHeight : kbHeight)

    UIView.animateWithDuration(0.3, animations: {
        self.durationView.frame = CGRectOffset(self.durationView.frame, 0, movement)
    })
}

来源:https://stackoverflow.com/questions/31504240/keyboardwillshow-only-moves-container-uiview-for-certain-uitextfields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!