【问题标题】:Dynamic UILabel size for view before uitableviewuitableview 之前视图的动态 UILabel 大小
【发布时间】:2016-07-03 16:47:29
【问题描述】:

我在这里尝试使此帖子描述标签根据 IOS9 上的内容大小增长和缩小时遇到了一些麻烦。我有一个视图(我将其称为 topView),我将其用作 tableview 的标题(所以当我向上滚动时,标题会消失)。在 topView 内部,有一堆堆栈视图。我希望根据内容大小增加和缩小帖子描述标签的高度。我确实知道如何在所有东西都在原型单元格内的简单情况下执行此操作(即设置估计的行高并设置 uitableviewautomaticDimensions,在标签上设置 sizetoFit 并将行数更改为 0)。但是,这是一种不同的情况,因为帖子描述标签实际上并不在单元格内,而是在表格视图单元格之前的视图中。

请注意,视图中的所有项目都具有静态高度,除了 postdescription 标签。帖子描述标签位于仅左右固定的堆栈视图内(所以顶部和底部会增长?)。此外,包含所有元素的主堆栈视图被固定在四个侧面,包含主堆栈视图的顶视图也被固定在四个侧面。通过这种设置,我希望顶视图会根据内容大小而增长和缩小。但是,我在输出中看不到这一点。我不知道是持有标签拒绝增长的stackview还是拒绝增长的顶视图为标签的stackview留出更多空间。谢谢

更新

感谢 Riadluke,我尝试按照建议做一些事情,即在计算所需高度后调整 headerview 的大小。我已将以下代码放在 viewDidLayoutSubview 中,它可以解决问题

postDescriptionLbl.sizeToFit()


let headerView = commentTableView.tableHeaderView!

headerView.setNeedsLayout()
headerView.layoutIfNeeded()

let height = TopStackView.frame.size.height + ImageStackView.frame.size.height + postDescriptionLbl.frame.size.height + SpacerStackView.frame.size.height + BottomStackView.frame.size.height
 headerView.frame.size.height = height
commentTableView.tableHeaderView = headerView

我现在使用这种方法遇到的问题是,当视图控制器出现时,我可以实际看到 postDescription 标签高度从故事板中的默认高度增长到所需的高度。例如,当 VC 首次出现时,我看到一行标签和一些字符串被切断,但是在 0.5 秒后,标题视图和标签增长到我想要的大小。我知道这是意料之中的,因为我在 viewDidLayout 子视图之后调用了操作。我想知道是否有更好的方法让我看不到这种过渡,并且视图看起来立即是正确的高度。 IE。让视图确切地知道标签有多高,以确定 headerview 在出现在屏幕上之前需要多高?

【问题讨论】:

    标签: ios swift dynamic autolayout uilabel


    【解决方案1】:

    恐怕UITableView 的视图设置为tableHeaderView 不会自动调整大小。它的高度将固定为它在 IB 中的高度。 您需要做的是手动设置它的大小,然后将其重新分配为 tableHeaderView,以便以您想要的高度显示。

    由于您使用的是自动布局,因此可能只需要几行代码。 您可以在设置标题视图的内容后立即尝试此代码:

        //for the target size you have set the width as your tableView's width when it is already displayed on screen.
        //note that when it is accesed inside viewDidLoad the tableView's bounds
        //may be different to the actual bounds it will be displayed with,
        //here I am just using the screen bounds
        let targetSize = CGSize(width: UIScreen.mainScreen().bounds.width, height: 10000)
    
        //set the tableHeader's size to its size after its layout constraints are resolved
        tableHeader.bounds.size =  tableHeader.systemLayoutSizeFittingSize(targetSize)
    
        //reassign it as the tableHeaderView to update the height it will be displayed in
        tableView.tableHeaderView = tableHeader
    

    【讨论】:

    • 感谢 riahluke 的回复。对上面的更新有任何想法吗?谢谢
    • 你试过systemLayoutFittingSize并在viewDidLoad中调用它吗?
    【解决方案2】:

    经过多次尝试,我无法使用原始设置进行任何操作。我取得的最好成绩是在视图确实出现后调整它的大小,这与您看到以前的布局不同。

    它现在正在使用一种完全不同的方法。我创建了两个原型单元,其中一个作为“HeaderViewCell”并实现了以下功能

    commentTableView.estimatedSectionHeaderHeight = 400
    commentTableView.sectionHeaderHeight = UITableViewAutomaticDimension
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    

    在那之后一切都像魅力一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 2015-11-23
      • 2021-01-21
      • 1970-01-01
      相关资源
      最近更新 更多