我正在尝试调整tableview的高度。我想在键盘消失时调整它。 tableview高度应该增加。此外,任何有数据的单元格也应该向下移动。它们应该向下移动以反映tableview的新高度。我怎么能这样做?
这是我现在的代码:
@IBOutlet weak var tableView: UITableView!
var items = ["Apple", "Orange", "Banana", "Pear"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? UITableViewCell else {
fatalError("The dequeued cell is not an instance of UITableViewCell.")
}
var item = items[indexPath.row]
cell.Label.text = item
//Some adjustments
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
答案 0 :(得分:0)
通过将约束从xib文件拖放到类文件,按如下方式连接tableview的底部约束。 (当没有键盘可见时,此底部约束是tableview的底部)
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
添加如下所示的键盘观察器
func setKBObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
根据键盘的可见性修改高度,如下所示
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
bottomConstraint.constant = keyboardSize.height
view.setNeedsLayout()
view.layoutIfNeeded()
}
}
func keyboardWillHide(notification: NSNotification) {
bottomConstraint.constant = 0
view.setNeedsLayout()
view.layoutIfNeeded()
}
答案 1 :(得分:0)
var isKeyboardOpen:Bool = false
@IBOutlet weak var tblheight: NSLayoutConstraint!
override func viewWillLayoutSubviews() {
super.updateViewConstraints()
let screenHeight = UIScreen.main.bounds.height
if self.tableView.contentSize.height > screenHeight{
if isKeyboardOpen == true{
self.tblheight.constant = screenHeight - 320
}else{
self.tblheight.constant = screenHeight - 100
}
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
self.viewWillLayoutSubviews()
}
// textField Delegate Method..
func textFieldDidBeginEditing(_ textField: UITextField) {
isKeyboardOpen = true
viewWillLayoutSubviews()
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.view.endEditing(true)
isKeyboardOpen = false
textField.resignFirstResponder()
viewWillLayoutSubviews()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
isKeyboardOpen = false
self.view.endEditing(true)
textField.resignFirstResponder()
return true
}
尝试这个..它对我有用。 希望它会有所帮助..