AutoLayout问题Swift 3.0 [英] AutoLayout issue Swift 3.0

查看:104
本文介绍了AutoLayout问题Swift 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用过Autolayout此屏幕截图.我想当我单击textView时,textView将始终位于键盘上方,并且我正在使用自定义NavigationBar.我已经使用过IQKeyBoardManagerSwift,它可以工作,但是我的NavigationBar也向上移动,如果我单击textView,我希望我的NavigationBar停留在顶部.预先感谢

I have already Autolayout this screenshot using.I want when i click textView,textView will always just above Keyboard and also i am using custom NavigationBar.I already used IQKeyBoardManagerSwiftIt is working but my NavigationBar also moves up I want my NavigationBarto be stick at top if i click textView.Any Solutions to this. thanks in advance

推荐答案

Swift 5.0 :-将UITextView拖动到contentView(UIView)中,创建contentView底部约束的IBOutlet,即bottomConstraint.使用上述代码后,自定义NavigationBar也将停留在顶部,只有textView会位于键盘上方.

Swift 5.0 :- Drag your UITextView in a contentView(UIView), Create IBOutlet of bottom constraint of contentView i.e bottomConstraint. After use the below code as mentioned and custom NavigationBar will also stick at top only textView will be just above keyboard.

 override func viewDidLoad() {
    super.viewDidLoad()
    let center: NotificationCenter = NotificationCenter.default
    center.addObserver(self, selector: #selector(Profile.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    center.addObserver(self, selector: #selector(Profile.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

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

@objc func keyboardWillHide(notification: NSNotification){
    UIView.animate(withDuration: 0.2, animations: { () -> Void in
        self.bottomConstraint.constant = 0
        self.view.layoutIfNeeded()
    })
}

这篇关于AutoLayout问题Swift 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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