gpt4 book ai didi

ios - systemLayoutSizeFittingSize 在第一次调用时给出了错误的尺寸

转载 作者:搜寻专家 更新时间:2023-10-30 22:18:58 27 4
gpt4 key购买 nike

我正在尝试根据约束计算 TableView 标题的高度。当我使用 layoutMarginsGuide 属性时,我在调用 systemLayoutSizeFittingSize 时得到了错误的尺寸。如果我在不使用边距指南的情况下固定边缘,它就可以工作。

代码如下:

class SomeVC: UIViewController, UITableViewDataSource, UITableViewDelegate {

// MARK: Properties

let tableView = UITableView()
let headerView = UIView()
let circle = UIView()
let circleSize: CGFloat = 100



// MARK: Methods

override func viewDidLoad() {
super.viewDidLoad()

tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cellID")
view.addSubview(tableView)

headerView.layoutMargins = UIEdgeInsetsMake(20, 20, 20, 20)
headerView.backgroundColor = UIColor.grayColor().colorWithAlphaComponent(0.36)

circle.backgroundColor = UIColor.grayColor()
circle.layer.cornerRadius = circleSize/2
headerView.addSubview(circle)


// Constraints for circle

let margins = headerView.layoutMarginsGuide

circle.translatesAutoresizingMaskIntoConstraints = false
circle.topAnchor.constraintEqualToAnchor(margins.topAnchor).active = true
circle.bottomAnchor.constraintEqualToAnchor(margins.bottomAnchor).active = true
circle.centerXAnchor.constraintEqualToAnchor(margins.centerXAnchor).active = true
circle.widthAnchor.constraintEqualToConstant(circleSize).active = true
circle.heightAnchor.constraintEqualToConstant(circleSize).active = true


// Constraints for tableView

tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraintEqualToAnchor(view.topAnchor).active = true
tableView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
tableView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor).active = true
tableView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true


// Calculate size for headerView considering all constraints

let size = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
headerView.frame = CGRect(origin: CGPointZero, size: size)
tableView.tableHeaderView = headerView

let size2 = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
print("size:", size.height) // prints wrong height - 100.0
print("size2:", size2.height) // prints correct height - 140.0
}


}

为什么当我调用 systemLayoutSizeFittingSize 第二次时它给出了正确的尺寸?

最佳答案

所以我认为正在发生的事情是,尽管您的约束正在通过您的代码进行更新,但 tableView 的实际布局并未根据您的约束进行重新更新。它当然会在 View 出现之前更新,但是为了在计算中使用,您需要更新 viewDidLoad 中的布局。尝试

let size2 = headerView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)

tableView.setNeedsLayout()
tableView.layoutIfNeeded()

print("size:", size.height) // prints wrong size - 100.0
print("size2:", size2.height) // prints correct size - 140.0

我会在您的打印功能之前调用它进行测试。更新 tableView 布局后,它应该为 tableView 及其 subview (例如标题)等框架返回正确的大小。

关于ios - systemLayoutSizeFittingSize 在第一次调用时给出了错误的尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33736813/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com