【问题标题】:AutoLayout issue Swift 3.0自动布局问题 Swift 3.0
【发布时间】:2018-09-15 03:29:07
【问题描述】:

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

【问题讨论】:

  • 消息滚动你用过scrollview吗?或者您的自定义导航栏是在滚动视图中还是在视图中?
  • 我也尝试了我的所有视图,除了滚动视图中的自定义导航,但自动布局仍然不正确@CodeChanger你能帮我解决这个问题
  • 我在 tableview 上显示来自 json 的消息以及当我向 textview 输入文本时它从 api 添加到 tableview 我的自定义导航栏在 View 中
  • @anujtiwari 我认为你应该在这个控制器中禁用 iqmanger 并使用类似 stackoverflow.com/questions/31356293/… 的约束

标签: swift3 autolayout uinavigationbar iqkeyboardmanager


【解决方案1】:

Swift 5.0 :- 将UITextView 拖到contentView(UIView) 中,创建contentView 底部约束的IBOutlet,即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()
    })
}

【讨论】:

    【解决方案2】:

    你可以用类似的方式实现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 中观察。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多