自动退出问题Swift 3.0

时间:2018-04-05 10:31:07

标签: swift3 autolayout uinavigationbar iqkeyboardmanager

enter image description here

我已经Autolayout使用了这个屏幕截图。我希望​​当我点击textView时,textView将始终位于键盘上方,我也使用自定义NavigationBar。我已经使用过{{1}它正在工作,但我的IQKeyBoardManagerSwift也向上移动我想让我的NavigationBar坚持到顶部,如果我点击textView.Any解决方案。提前谢谢

2 个答案:

答案 0 :(得分:2)

Swift 3.0 : - 将UITextView拖到contentView(UIView),创建内容查询底部约束IBOutletbottomConstraint。使用下面提到的代码后,自定义NavigationBar也会粘在顶部,只有textView会在键盘上方。

 override func viewDidLoad() {
    super.viewDidLoad()

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

func keyboardWillShow(notification: NSNotification){

    let userInfo:NSDictionary = notification.userInfo! as NSDictionary
    let keyboardSizeNow:CGSize = (userInfo.object(forKey: UIKeyboardFrameEndUserInfoKey)! as AnyObject).cgRectValue.size
    UIView.animate(withDuration: 0.2, animations: { () -> Void in
        self.bottomConstraint.constant = keyboardSizeNow.height - 49
        self.view.layoutIfNeeded()
    })
}
func keyboardWillHide(notification: NSNotification){
    UIView.animate(withDuration: 0.2, animations: { () -> Void in
        self.bottomConstraint.constant = 0
        self.view.layoutIfNeeded()
    })
}

答案 1 :(得分:0)

你可以用类似的方式实现keyboardWillShow和keyboardWillHide方法

func keyboardWillShow(notification: NSNotification) {

        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            buttonBottomConstraint.constant = keyboardSize.height
            UIView.animate(withDuration: 0.3, animations: {
                self.view.layoutIfNeeded()
            })
        }
    }
    func keyboardWillHide(notification: NSNotification) {
        if let _ = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            bottomConstraint.constant = 0
            UIView.animate(withDuration: 0.3, animations: {
                self.view.layoutIfNeeded()
            })
        }
    }

另外,请不要忘记在viewDidLoad中观察。