systemLayoutSizeFittingSize在首次调用时给出了不正确的大小

问题描述 投票:6回答:2

我正在尝试根据约束条件计算表格视图标题的高度。当我使用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 第二次会给出正确的大小?

ios swift autolayout
2个回答
0
投票

因此,我认为正在发生的事情是,尽管您的约束通过代码进行更新,但tableView的实际布局尚未根据您的约束进行更新。它当然会在视图出现之前进行更新,但是要用于计算中,您需要更新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及其子视图(例如标题)返回正确的大小。


0
投票

我与systemLayoutSizeFittingSize有类似的问题,尽管可能无关。

视图的安全区域插图尚未在第一次调用(它们全为0)时设置为systemLayoutSizeFittingSize,也许这也适用于布局边距。后续调用将正确设置安全插入集,这将导致正确的计算。

我认为,在您的情况下,唯一的解决方案是坚持不使用边距引导。

© www.soinside.com 2019 - 2024. All rights reserved.