【问题标题】:How do I adjust the size of a UITableView in viewDidLoad?如何在 viewDidLoad 中调整 UITableView 的大小?
【发布时间】:2021-10-17 00:40:34
【问题描述】:

我有一个包含两个元素的视图控制器:UITableView 和一个包含UITextField 的UIView。我也在使用IQKeyboardManager 库,以便我的UITextField 可见。下面是控制它们的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround()
    tableView.register(UINib(nibName: "CommentCell", bundle: nil), forCellReuseIdentifier: "CommentCell")
    tableView.dataSource = self
    self.view.addSubview(tableView)
    commentTextField.delegate = self
    NotificationCenter.default.addObserver(self,
                                               selector: #selector(handle(keyboardShowNotification:)),
                                               name: UIResponder.keyboardDidShowNotification,
                                               object: nil)
    let textFieldHeight = commentCompositionView.frame.size.height
    let navBarHeight = navigationController?.navigationBar.frame.size.height ?? 0.0
    tableView.frame = CGRect(x: 0, y: keyboardHeight, width: screenSize.width, height: screenSize.height - (textFieldHeight + navBarHeight + keyboardHeight + 20))
    print("Did load: \(tableView.frame.size.height)")
}
    
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    commentTextField.becomeFirstResponder()
    print("Did Appear: \(tableView.frame.size.height)")
}
    
    
    
func textFieldDidBeginEditing(_ textField: UITextField) {
    let textFieldHeight = commentCompositionView.frame.size.height
    let navBarHeight = navigationController?.navigationBar.frame.size.height ?? 0.0
    tableView.frame = CGRect(x: 0, y: keyboardHeight, width: screenSize.width, height: screenSize.height - (textFieldHeight + navBarHeight + keyboardHeight + 20))
    print("Did begin: \(tableView.frame.size.height)")
}
    
func textFieldDidEndEditing(_ textField: UITextField) {
    let textFieldHeight = commentCompositionView.frame.size.height
    let navBarHeight = navigationController?.navigationBar.frame.size.height ?? 0.0
    tableView.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height - (textFieldHeight + navBarHeight + 20))
    print("Did end: \(tableView.frame.size.height)")
}

我的问题是当视图加载时UITableView 的高度太大,我看不到表格视图的顶部。当我点击退出然后回到UITableView 时,它工作得很好,但第一次加载不起作用。

这里是视图加载的输出示例,我点击了UITextField,然后点击返回:

Did load: 465.0
Did begin: 465.0
Did Appear: 465.0
Did end: 465.0
Did begin: 211.0
Did end: 465.0
Did begin: 211.0

在我看来,鉴于我在viewDidLoad 中设置高度,前三个打印语句应该改为211.0。我做错了什么?

编辑

我找到了问题所在,但不确定如何解决。当视图首次加载时keyboardHeight 为 0.0。直到我关闭并重新打开键盘,它才会给我一个准确的数字。

我正在使用this 的答案来获取keyboardHeight

【问题讨论】:

  • 您是否尝试过在viewDidLayoutSubviews 中实现您的布局代码?这很可能是由于在您请求尺寸时视图布局不正确
  • @Bram 我刚刚尝试将我设置tableView.frame 的行从viewDidLoad 移动到viewDidLayoutSubviews,我得到了相同的结果。
  • 我在viewWillLayoutSubviews也试过了,同样的问题
  • @David 您的表格视图是如何创建的?如果它在故事板中而不是你不能改变它的框架,你需要设置你的约束
  • @Philip 我的表格视图是以编程方式创建的。我可以改变它的大小。问题是大小没有在正确的时间改变。

标签: swift uitableview uitextfield iqkeyboardmanager


【解决方案1】:

我的问题是我需要在viewDidAppear 中设置UITableView 高度,一旦我这样做了,我的问题就解决了:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    commentTextField.becomeFirstResponder()
    let textFieldHeight = commentCompositionView.frame.size.height
    let navBarHeight = navigationController?.navigationBar.frame.size.height ?? 0.0
    tableView.frame = CGRect(x: 0, y: keyboardHeight, width: screenSize.width, height: screenSize.height - (textFieldHeight + navBarHeight + keyboardHeight + 20))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    相关资源
    最近更新 更多