如果我给故事板中的超级视图迅速赋予底部约束,为什么视图不带键盘?

问题描述 投票:0回答:1

我正在聊天页面上,在这里我在bottomview中添加了文本字段

如果我将botomview底部约束赋予情节提要中的Superview:那么,bottomview为何不带键盘?

 leading, trailling = 0 , bottom to Superview = 0, height = 80

如果我将botomview底部约束赋予情节提要中的安全区域:则bottomview将随键盘一起出现。

leading, trailling = 0 , bottom to Safe Area = 0, height = 80

如果我没有对超级视图赋予底部约束,那么我在iPhone 11中的键盘和底部视图之间就会出现间隙

enter image description here

 import UIKit

 class DealsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

 @IBOutlet weak var tableView: UITableView!
 @IBOutlet weak var customView: UIView!
 @IBOutlet weak var bottomviewBottomConstraint: NSLayoutConstraint!

  var itemsArr = ["one", "2", "3", "4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","32","33","34","35","36","37","38","39",]
 override func viewDidLoad() {
     super.viewDidLoad()
     //self.title = "TextField"

     NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: UIResponder.keyboardWillShowNotification, object: nil)

     NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardNotification), name: UIResponder.keyboardWillHideNotification, object: nil)
 }


 override func viewWillDisappear(_ animated: Bool) {
     NotificationCenter.default.removeObserver(self)
 }

 @objc func handleKeyboardNotification(_ notification: Notification) {

     if let userInfo = notification.userInfo {

         let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue

         let isKeyboardShowing = notification.name == UIResponder.keyboardWillShowNotification
         bottomviewBottomConstraint?.constant = isKeyboardShowing ? -keyboardFrame!.height : 0

         UIView.animate(withDuration: 0.5, animations: { () -> Void in
             self.view.layoutIfNeeded()
         })
     }
 }

 override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
     self.customView.superview?.setNeedsLayout()
     //self.customView.superview?.layoutIfNeeded()
 }

请帮助我解决此问题。

我正在聊天页面上,如果我在Storyboard中为Superview赋予了botomview底部约束,则在bottomview内添加了文本字段:那么bottomview没有带键盘为什么? ...

swift view keyboard constraints
1个回答
0
投票

您不应该-您的值应该添加它,因为它是出口而不是锚点。

© www.soinside.com 2019 - 2024. All rights reserved.