键盘出现时移动视图并将其显示在scrollView中。斯威夫特3

时间:2016-10-26 02:08:31

标签: ios uiscrollview keyboard swift3

关于SO有几个类似的问题,但是没有解决下面的问题。

我正在使用简单的登录视图,只有2个textFields用于电子邮件和密码。 最好的用户体验是用户点击其中一个textFields,键盘弹出,视图向上移动,同时成为scrollView。 这样,用户仍然可以看到屏幕上的其他UI元素。 用户还应该可以通过向下滑动来隐藏键盘。 (Instagram和其他大男孩已经实现了这一点)

我能够构建一个没有scrollView的视图,并通过resignFirstResponder关闭键盘:

class SignInVC: UIViewController, UITextFieldDelegate {

@IBOutlet weak var pwdField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var scrollView: UIScrollView!

var keyboardDismissTapGesture: UIGestureRecognizer?

func dismissKeyboard(sender: AnyObject) {
    pwdField?.resignFirstResponder()
    emailField?.resignFirstResponder()
}

override func viewDidLoad() {
    super.viewDidLoad()        
}

override func viewDidAppear(_ animated: Bool) {

    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

func keyboardWillShow(notification: NSNotification) {

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0{
            self.view.frame.origin.y -= keyboardSize.height  
        }
    }

    if keyboardDismissTapGesture == nil
    {
        keyboardDismissTapGesture = UITapGestureRecognizer(target: self,
                                                           action: #selector(self.dismissKeyboard))
        self.view.addGestureRecognizer(keyboardDismissTapGesture!)
    }
}

func keyboardWillHide(notification: NSNotification) {

    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != 0{
            self.view.frame.origin.y += keyboardSize.height
        }
    }

    if keyboardDismissTapGesture != nil
    {
        self.view.removeGestureRecognizer(keyboardDismissTapGesture!)
        keyboardDismissTapGesture = nil
    }
}

override func viewWillDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self)

    super.viewWillDisappear(animated)
}

我试图与scrollView.contentSize试图同时移动它的起源:     scrollView.frame.origin.y -= keyboardSize.height

或其contentSize:      scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height - keyboardSize.height)

但没有任何效果。

关于键盘的刷卡功能,有一些信息,在scrollView的属性检查器中,我可以更改键盘"键盘"财产到"交互式解雇"。 没有工作。

1 个答案:

答案 0 :(得分:1)

//声明一个包含你的activetextfiled的uitextfield

override func viewDidLoad() {
    super.viewDidLoad()   
 keyboardDismissTapGesture = UITapGestureRecognizer(target: self,                                                           action:      #selector(self.dismissKeyboard))     
}

//在viewdidload中添加键盘关闭点击手势

func textFieldDidBeginEditing(textField: UITextField) {
activeTextField  = textField

    }

func textFieldDidEndEditing(textField: UITextField) {

  activeTextField  = nil
}

//在textfield中设置活动文本字段确实开始委托

func dismissKeyboard(){
   activeTextField.resignFirstResponder()
}

//关闭activeTextField的键盘

func keyboardWillShow(notification:NSNotification){

    var userInfo = notification.userInfo!
    var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
    keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil)

    var contentInset:UIEdgeInsets = self.scrollView.contentInset
    contentInset.bottom = keyboardFrame.size.height
    self.scrollView.contentInset = contentInset
}

//键盘显示时调整scrollview内容大小

func keyboardWillHide(notification:NSNotification){

    let contentInset:UIEdgeInsets = UIEdgeInsetsZero
    self.scrollView.contentInset = contentInset
}

//键盘移动时重置scrollview

Company |   Product |       |Total Sales
AAA     QQQ     123
BBB     QQQ     140
CCC     WWW     127
DDD     WWW     145
CCC     QQQ     190
DDD     QQQ     290
AAA     WWW     240
BBB     WWW     120
AAA     RRR     123
BBB     RRR     122
CCC     RRR     178
DDD     RRR     789