键盘覆盖时向上移动文本字段

时间:2014-06-11 18:57:36

标签: ios swift uikit

我对Objective C并不熟悉,并且想知道Swift中是否有解决方案。

这是我正在玩的非常简单的SingleView应用程序。 UIView继承自UIControl。

我没有UIScrollView,我不能只移动整个视图吗?我相信键盘是独立的。

4 个答案:

答案 0 :(得分:12)

Apple的推荐记录在Swift中。

确保您的控件位于ScrollView上,并在选择器字符串中记下冒号。我使用变量(activeField)来指示活动文本字段。

var activeField: UITextField?

@IBAction func editingBegan(sender: UITextField) {
    activeField = sender
}

 override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    let center = NSNotificationCenter.defaultCenter()
    center.addObserver(self, selector: "keyboardOnScreen:", name: UIKeyboardDidShowNotification, object: nil)
    center.addObserver(self, selector: "keyboardOffScreen:", name: UIKeyboardDidHideNotification, object: nil)

}

func keyboardOnScreen(notification: NSNotification){
    let info: NSDictionary  = notification.userInfo!
    let kbSize = info.valueForKey(UIKeyboardFrameEndUserInfoKey)?.CGRectValue().size
    let contentInsets:UIEdgeInsets  = UIEdgeInsetsMake(0.0, 0.0, kbSize!.height, 0.0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
    var aRect: CGRect = self.view.frame
    aRect.size.height -= kbSize!.height
    //you may not need to scroll, see if the active field is already visible
    if (!CGRectContainsPoint(aRect, activeField!.frame.origin) ) {
        let scrollPoint:CGPoint = CGPointMake(0.0, activeField!.frame.origin.y - kbSize!.height)
        scrollView.setContentOffset(scrollPoint, animated: true)
    }

}


func keyboardOffScreen(notification: NSNotification){
    let contentInsets:UIEdgeInsets = UIEdgeInsetsZero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}

答案 1 :(得分:7)

我在这里使用了代码。不需要添加scrollview

Move uitextfield up when keyboard shows

func textFieldDidBeginEditing(textField: UITextField) {
        animateViewMoving(true, moveValue: 100)
}
func textFieldDidEndEditing(textField: UITextField) {
        animateViewMoving(false, moveValue: 100)
}

func animateViewMoving (up:Bool, moveValue :CGFloat){
    var movementDuration:NSTimeInterval = 0.3
    var movement:CGFloat = ( up ? -moveValue : moveValue)
    UIView.beginAnimations( "animateView", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(movementDuration )
    self.view.frame = CGRectOffset(self.view.frame, 0,  movement)
    UIView.commitAnimations()
}

答案 2 :(得分:1)

https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

查看"接收键盘通知"。

该文档中的代码示例都是Obj-C,因此您必须自己将其转换为swift,但正如@jtbandes所说,它本身并不是一个快速的问题。

答案 3 :(得分:0)

我刚刚将上面的ChinLoong答案更新为Swift 3语法。不幸的是,我还不允许添加评论。

func textFieldDidBeginEditing(_ textField: UITextField) {
    animateViewMoving(up: true, moveValue: 100)
}
func textFieldDidEndEditing(_ textField: UITextField) {
    animateViewMoving(up: false, moveValue: 100)
}

func animateViewMoving (up:Bool, moveValue :CGFloat){
    let movementDuration:TimeInterval = 0.3
    let movement:CGFloat = ( up ? -moveValue : moveValue)
    UIView.beginAnimations( "animateView", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(movementDuration )
    self.view.frame = self.view.frame.offsetBy(dx: 0,  dy: movement)
    UIView.commitAnimations()
}