出现键盘时移动视图,并将其显示在scrollView中.斯威夫特3 [英] Move View when keyboard appears and present it in a scrollView. Swift 3

查看:89
本文介绍了出现键盘时移动视图,并将其显示在scrollView中.斯威夫特3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于SO有几个类似的问题,但未解决以下问题.

There are several similar questions on SO but non addresses the issue below.

我正在使用一个简单的登录视图,该视图只有2个用于电子邮件和密码的文本字段. 最好的UX是当用户单击textFields之一时,弹出键盘,并且视图向上移动,同时成为scrollView. 这样,用户仍然可以看到屏幕上还有哪些其他UI元素. 用户还应该能够通过向下滑动来隐藏键盘. (Instagram和其他大男孩已经实施了此功能)

I'm working on a simple login view with just 2 textFields for email and password. The best UX is when a user taps one of the textFields, the keyboard pops in, and the view moves up, while becoming a scrollView. This way, a user can still see what other UI elements are on the screen. User should also be able to hide the keyboard by swiping down. (Instagram and other big boys have this implemented)

我能够在没有scrollView的情况下构建视图,并通过resignFirstResponder关闭键盘:

I was able to built a view without a scrollView and dismiss the keyboard by 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

I was trying to play around with scrollView.contentSize trying to also move its origin: scrollView.frame.origin.y -= keyboardSize.height

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

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

但没有任何效果.

关于键盘的向下滑动功能,有一些信息表明,在scrollView的属性检查器中,我可以将键盘"属性更改为以交互方式关闭". 没用

Regarding the keyboard's ability to be swiped down, there was information that in scrollView's attribute inspector, I could change the "Keyboard" property to "Dismiss Interactively". Didn't work.

推荐答案

//声明一个uitextfield,用于保存您的activetextfiled

// declare a uitextfield which holds your activetextfiled

var activeTextField:UITextField!

///在您的viewdidload中添加键盘消除点击手势

// add keyboard dismiss tap gesture in your viewdidload

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

//在文本字段中设置活动文本字段确实开始删除

// set active text field in textfield did begin delegte

func textFieldDidBeginEditing(textField: UITextField) {
activeTextField  = textField

    }

func textFieldDidEndEditing(textField: UITextField) {

  activeTextField  = nil
}

//关闭activeTextField的键盘

// dismiss keyboard for the activeTextField

func dismissKeyboard(){
   activeTextField.resignFirstResponder()
}

//调整显示键盘时滚动视图内容的大小

// adjust scrollview content size when keyboard will show

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
}

///键盘消失时重置滚动视图

// reset scrollview when keyboard will go

func keyboardWillHide(notification:NSNotification){

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

这篇关于出现键盘时移动视图,并将其显示在scrollView中.斯威夫特3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆