出现键盘时,我正在使用滚动视图向上滚动视图。
this is how the controller looks
下一张图片是我第一次激活键盘时控制器的外观。我的问题是大标题消失了。我设置了倾向于在确实加载的视图中将大标题设置为true,并且大标题显示模式始终为=。,但是看起来滚动视图中的视图向上移动,从而使导航栏变小了。
this is how the controller looks when I dismiss the view interactively using the tableview
下一张图片是第二次单击文本字段后发生的情况。文本字段显示在键盘下方,导航栏仍然很小。我需要像第二个图像一样的文本字段,并且我需要导航栏始终较大。
这是我的代码:
class ChatVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var sendMessageView: UIView!
@IBOutlet var superViewContainer: UIView!
@IBOutlet weak var scrollView: UIScrollView!
var Messages = [Message]()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
self.navigationItem.largeTitleDisplayMode = UINavigationItem.LargeTitleDisplayMode.always
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .always
let m1 = Message(senderName: "Chris", message: "This is my new message, iejiejdijaidnjadnjasndjanjdjdjandjjdnjasnjdsadnsnajdnjasdnjsnkkmdklllll")
let m2 = Message(senderName: "John ", message: "This is read")
Messages.append(m1)
Messages.append(m2)
Messages.append(m1)
Messages.append(m2)
Messages.append(m1)
Messages.append(m2)
Messages.append(m1)
Messages.append(m2)
Messages.append(m1)
Messages.append(m2)
print(Messages)
NotificationCenter.default.addObserver(self, selector: #selector(ChatVC.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ChatVC.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
if #available(iOS 11, *) {
UIScrollView.appearance().contentInsetAdjustmentBehavior = .never
}
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: self.view.window)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: self.view.window)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Messages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: chatCellId) as! ChatCell
cell.messageLbl.text = Messages[indexPath.row].message
return cell
}
@objc func keyboardWillShow(notification: NSNotification) {
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
keyboardFrame = self.view.convert(keyboardFrame, from: nil)
var contentInset:UIEdgeInsets = self.scrollView.contentInset
contentInset.bottom = keyboardFrame.size.height - 20
scrollView.contentInset = contentInset
}
@objc func keyboardWillHide(notification: NSNotification) {
let contentInset:UIEdgeInsets = UIEdgeInsets.zero
scrollView.contentInset = contentInset
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}}
移动视图的代码是keyboardWillShow()函数