【问题标题】:UITextField move up when keyboard appears in Swift当键盘出现在 Swift 中时 UITextField 向上移动
【发布时间】:2015-10-22 06:39:14
【问题描述】:

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

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
}

【问题讨论】:

标签: ios swift uitextfield keyboard-events


【解决方案1】:

您是否在 ViewController 类中分配了 UITextField 代表? 如果不设置为self。

self.youttextfield.delegate = self

【讨论】:

  • @Mohammad 解决了你的问题吗?请提及,以便我们为您提供更多解决方案。
  • 感谢@Israr 的帮助,您是对的,但我在登录页面中使用了没有“UITextFieldDelegate”的代码并且效果很好,
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2010-12-19
    • 2019-04-04
    相关资源
    最近更新 更多