【问题标题】:Adjust the height of scrollview according to the height of his child tableview in SWIFT根据他在SWIFT中子tableview的高度调整scrollview的高度
【发布时间】:2015-02-28 08:49:22
【问题描述】:

我有一个 viewController,在滚动视图中有一个 tableview。

我需要以编程方式更新 tableview 的高度,然后是 scrollview 的高度(scrollview 的高度取决于 tableview 的高度)。

-my View Controller
  -View
    -ScrollView
      -Container View
        -TableView

所以我在 viewController 的 viewDidAppear 中写了这段代码:

 override func viewDidAppear(animated: Bool) {

    super.viewDidAppear(animated)

    var frame:CGRect = self.tableView.frame
    frame.size.height = tableView.contentSize.height
    self.tableView.frame = frame

    scrollView.contentSize=CGSizeMake(scrollView.frame.size.width, tableView.frame.origin.y + tableView.frame.height)

 }

如果我在控制台中检查,viewDidAppear 会在所有 cellForRowAtIndexPath 之前调用。在这种情况下,一切都按我的意愿进行。

在我的 viewController 中,我有一个打开 modalViewController 的按钮。在这个 modalViewController 中,我在我的 tableview 中添加了一个新数据(所以,一个新单元格)。

当我使用dismissViewControllerAnimated 关闭我的modalViewController 时,会调用viewDidAppear,但在所有cellForRowAtIndexPath 之后。 我的 viewController 显示不正确,tableview 被裁剪,我无法滚动到 tableView 的底部。我的代码不起作用。

我正在使用 Swift 和自动布局。

【问题讨论】:

    标签: ios uitableview swift uiscrollview autolayout


    【解决方案1】:

    尝试在您的视图控制器中调用它:

    automaticallyAdjustsScrollViewInsets = false
    

    如果您愿意,也可以在 Interface Builder 中设置它

    【讨论】: