键盘显示或隐藏时调整UIView

时间:2015-12-16 14:04:31

标签: ios swift2 uikeyboard

我正在添加键盘观察器以在键盘显示时调整视图高度。但是当文本字段位于视图的顶部并且键盘显示时,文本字段会更多。有人可以指导我如何妥善解决这个问题,因为在进行了大量搜索之后,他们都提出了同样的建议。我在iPhone 5上使用iOS 9。

这是我的代码。在viewDidLoad()

//SET OBSERVER WHEN KEYBOARD IS SHOWN
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: self.view.window)

//SET OBSERVER WHEN KEYBOARD IS HIDDEN
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: self.view.window)

override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: self.view.window)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: self.view.window)
}

//HANDLING KEYBOARD APPEARING AND DISAPPEARING
func keyboardWillHide(sender: NSNotification) {
    let userInfo: [NSObject : AnyObject] = sender.userInfo!
    let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
    self.view.frame.origin.y += keyboardSize.height
}

func keyboardWillShow(sender: NSNotification) {
    let userInfo: [NSObject : AnyObject] = sender.userInfo!
    let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
    let offset: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue.size

    print(("\(keyboardSize.height)---\(offset.height)"))  **//this is always showing the values 216.0 for both** 

    if keyboardSize.height == offset.height {
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.view.frame.origin.y -= keyboardSize.height
        })
    } else {
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.view.frame.origin.y += keyboardSize.height - offset.height
        })
    }
}

1 个答案:

答案 0 :(得分:3)

你正在采取一种复杂的方法来处理不是一个非常困难的问题。请记住,手机有各种尺寸,因此尝试移动视图可能对一台设备(例如iPhone 5)很有用,但对其他设备(例如iPhone 4,iPhone 6s)的体验很差。

您正在寻找的解决方案是将控件置于UIScrollView内。然后,当键盘显示或隐藏时,您不会移动任何 - 您只需调整滚动视图的内容插入,然后让iOS完成其余工作。

显示和隐藏的键盘很复杂,因为还有可以随意连接和拔出的硬件键盘;在我的(极其广泛的!)测试中,无论键盘发生了什么,我都设法将所有条件下看似行为正常的代码整合在一起。我试图将它修剪得更小,并且总是找到一个边缘情况,它停止工作,所以我已经解决了这个固定的方法,它的效果很好。如果您能够使用UIScrollView,这种确切的方法也适用于您。

第1步:注册正确的通知。具体来说,将其放入viewDidLoad()

let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "adjustForKeyboard:", name: UIKeyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: "adjustForKeyboard:", name: UIKeyboardWillChangeFrameNotification, object: nil)

第2步:将此方法添加到视图控制器:

func adjustForKeyboard(notification: NSNotification) {
    let userInfo = notification.userInfo!

    let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)

    if notification.name == UIKeyboardWillHideNotification {
        yourScrollView.contentInset = UIEdgeInsetsZero
    } else {
        yourScrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
    }

    yourScrollView.scrollIndicatorInsets = yourScrollView.contentInset
}

第3步:使用UITextView的任何人都应该将这两行添加到该方法的末尾,这样用户就不会失去位置:

let selectedRange = yourScrollView.selectedRange
yourScrollView.scrollRangeToVisible(selectedRange)

......虽然在这种情况下可能是yourTextView或类似的。