自动布局问题 Swift 3.0 [英] AutoLayout issue Swift 3.0

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

问题描述

我已经 Autolayout 这个屏幕截图使用了.我希望当我点击 textView 时,textView 将始终位于键盘上方,而且我正在使用自定义 NavigationBar.我已经使用 IQKeyBoardManagerSwift它正在工作,但我的 NavigationBar 也向上移动我希望我的 NavigationBar 在单击 textView 时保持在顶部.对此的任何解决方案.在此先感谢

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

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

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