当我尝试到打开键盘我的全部视图滚动向上.我只需要到滚动这tableview和textview.换句话说,我需要到保持导航条没有滚动向上.
我的视图堆栈是:
视图
- 导航栏视图
- 表格视图
- 文本视图
override func viewDidLoad() {
super.viewDidLoad()
self.adjustViews()
ChatTableSectionHeaderView.dateFormatter.dateStyle = .medium
ChatTableSectionHeaderView.dateFormatter.timeStyle = .none
MessageInboundCell.dateFormatter.dateStyle = .none
MessageInboundCell.dateFormatter.timeStyle = .short
self.attachButton.isHidden = true
self.textView.delegate = self
self.setUpSendButton(isDisabled: !isChatTextValid(text: self.textView.text))
self.tableView.tableFooterView = UIView(frame: .zero)
let igTextNib = UINib(nibName: MessageInboundCell.string, bundle: nil)
let igImageNib = UINib(nibName: MessageImageInboundCell.string, bundle: nil)
let sectionHeaderView = UINib(nibName: ChatTableSectionHeaderView.string, bundle: nil)
tableView.register(igTextNib, forCellReuseIdentifier: MessageInboundCell.string)
tableView.register(igImageNib, forCellReuseIdentifier: MessageImageInboundCell.string)
tableView.register(sectionHeaderView, forHeaderFooterViewReuseIdentifier: ChatTableSectionHeaderView.string)
tableView.delegate = self
tableView.dataSource = self
registerForKeyboardWillShowNotification(tableView)
registerForKeyboardWillHideNotification(tableView)
}
}
延伸
func registerForKeyboardWillShowNotification(_ scrollView: UIScrollView, usingBlock block: ((CGSize?) -> Void)? = nil) {
_ = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: nil, using: { notification -> Void in
let userInfo = notification.userInfo!
let keyboardSize = (userInfo[UIResponder.keyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue.size
let contentInsets = UIEdgeInsets(top: scrollView.contentInset.top, left: scrollView.contentInset.left, bottom: keyboardSize.height, right: scrollView.contentInset.right)
scrollView.setContentInsetAndScrollIndicatorInsets(contentInsets)
block?(keyboardSize)
})
}
func registerForKeyboardWillHideNotification(_ scrollView: UIScrollView, usingBlock block: ((CGSize?) -> Void)? = nil) {
_ = NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil, using: { notification -> Void in
let userInfo = notification.userInfo!
let keyboardSize = (userInfo[UIResponder.keyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue.size
let contentInsets = UIEdgeInsets(top: scrollView.contentInset.top, left: scrollView.contentInset.left, bottom: 0, right: scrollView.contentInset.right)
scrollView.setContentInsetAndScrollIndicatorInsets(contentInsets)
block?(keyboardSize)
})
}
}
1条答案
按热度按时间i34xakig1#