当键盘出现在Swift中时,UITextField会向上移动

时间:2015-10-22 06:39:15

标签: ios swift uitextfield keyboard-events

我使用此代码在键盘出现时向上移动视图,在我的登录页面中,此代码运行良好,但在注册页面中它不起作用。

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()
}

我还有另一个功能可以在键盘上返回键,它也可以在登录页面上工作,但不能在注册页面上工作。两页之间的所有内容都相同。

  func textFieldShouldReturn(textField: UITextField) -> Bool {

    textField.resignFirstResponder()
    return true
}

2 个答案:

答案 0 :(得分:4)

您是否已在ViewController类中分配了UITextField的委托? 如果没有把它设置为自我。

self.youttextfield.delegate = self

答案 1 :(得分:0)

演示链接:https://github.com/harshilkotecha/UIScrollViewWhenKeyboardAppearInSwift3

当你有多个textview时,它是如此困难,所以最好的解决方案 - >

第1步:添加UITextFieldDelegate

class ScrollViewController: UIViewController,UITextFieldDelegate {

第2步:创建新的IBOutlet,但不要连接任何文本字段

//  get current text box when user Begin editing
    @IBOutlet weak var activeTextField: UITextField?

步骤3:当用户关注文本字段对象传递引用并存储在activeTextField中时,编写这两个方法

// get current text field
    func textFieldDidBeginEditing(_ textField: UITextField)
    {
        activeTextField=textField;
    }
    func textFieldDidEndEditing(_ textField: UITextField)
    {
        activeTextField=nil;
    }

步骤5:在viewdidload setNotificationKeyboard中设置通知

 override func viewWillAppear(_ animated: Bool) {
            // call method for keyboard notification
            self.setNotificationKeyboard()
        }

        // Notification when keyboard show
        func setNotificationKeyboard ()  {
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: .UIKeyboardWillShow, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: .UIKeyboardWillHide, object: nil)
        }

第6步:隐藏和取消隐藏的两种方法

func keyboardWasShown(notification: NSNotification)
    {
        var info = notification.userInfo!
        let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
        let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height+10, 0.0)
        self.scrollView.contentInset = contentInsets
        self.scrollView.scrollIndicatorInsets = contentInsets
        var aRect : CGRect = self.view.frame
        aRect.size.height -= keyboardSize!.height
        if let activeField = self.activeTextField
        {
            if (!aRect.contains(activeField.frame.origin))
            {
                self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
            }
        }
    }
// when keyboard hide reduce height of scroll view


 func keyboardWillBeHidden(notification: NSNotification){
        let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0,0.0, 0.0)
        self.scrollView.contentInset = contentInsets
        self.scrollView.scrollIndicatorInsets = contentInsets
        self.view.endEditing(true)
    }