swift3 - 自动布局问题 Swift 3.0

标签 swift3 autolayout uinavigationbar iqkeyboardmanager

enter image description here

我已经使用了自动布局此屏幕截图。我希望​​当我单击textView时,textView将始终位于键盘上方,而且我正在使用自定义NavigationBar。我已经使用了 IQKeyBoardManagerSwift 它正在工作,但我的 NavigationBar 也向上移动,如果我单击 textView,我希望我的 NavigationBar 停留在顶部.任何解决方案。提前致谢

最佳答案

Swift 5.0 :- 将您的UITextView拖到contentView(UIView)中,创建底部约束的IBOutlet contentView 的,即 bottomConstraint。使用下面提到的代码后,自定义 NavigationBar 也将粘在顶部,只有 textView 将位于键盘上方。

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

关于swift3 - 自动布局问题 Swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49670145/

相关文章:

ios - 为 iPhone 和 iPad 布局静态表格单元格

ios - 自动在导航栏中添加 "back"按钮

iOS 右导航按钮不显示在 UINavigationBar 中

ios - iOS 上的 FaSTLane(Swift 3 和 XCode 8.2)

objective-c - NS_SWIFT_NAME 基本名称验证错误

arrays - fatal error : Index out of range (Arrays)

ios - View Controller 的 iPhone AutoLayout Root View 无法固定

ios - URLSession 产生 '()' ,而不是预期的上下文结果类型

ios - 如何在不使用代码的情况下使用约束 AutoLayout 在横向和纵向上指定不同的布局?

ios - 如何在 iOS 10 中使导航栏透明