【问题标题】:systemLayoutSizeFittingSize gives incorrect size on first callsystemLayoutSizeFittingSize 在第一次调用时给出不正确的尺寸
【发布时间】:2016-02-17 15:15:54
【问题描述】:

我正在尝试根据约束计算表格视图标题的高度。当我使用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


【解决方案1】:

所以我认为正在发生的是,尽管您的约束正在通过您的代码进行更新,但 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 及其子视图(如标题)。

【讨论】:

  • 您是在更新尺寸后打印出来之前添加的吗?
  • 是的@matthew-lawrence-bailey
  • 打印 - 尺寸:100.0 size2: 140.0
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多