SystemLayoutSizeFittingSize gives wrong size on first call

I am trying to calculate table header height based on constraints. When I use the property layoutMarginsGuide, I get the wrong size causing it systemLayoutSizeFittingSize. If I click the edges without using the guide field, it works.

Here is the code:

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
}


}

Why, when I call the systemLayoutSizeFittingSizesecond time, it gives the correct size?

+4
source share
1 answer

, , , , 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 , .

0

Source: https://habr.com/ru/post/1616060/


All Articles