Skip to content

Instantly share code, notes, and snippets.

@Heilum
Created May 17, 2020 05:28
Show Gist options
  • Save Heilum/49792a2545ed967583ad58fc9a730d4e to your computer and use it in GitHub Desktop.
Save Heilum/49792a2545ed967583ad58fc9a730d4e to your computer and use it in GitHub Desktop.
let targetWidth = self.view.bounds.size.width;
let targetSize = CGSize(width: targetWidth, height: UIView.layoutFittingCompressedSize.height)
//如果headView中有通过aspect ratio决定高度的constraint,这将导致systemLayoutSizeFittingSize计算错误,所以在代码中把这种constraint转为固定高度的constraint
//see : https://stackoverflow.com/questions/44801492/systemlayoutsizefitting-ignoring-layout-constraints-and-using-image-view-intrins
let imageView = self.empyHeadView.subviews.last!
imageView.constraintWithIdentifier("heightConstraint")!.constant = (targetWidth - 40) * CGFloat(0.3);
//宽度固定,高度可变
let resultSize = self.empyHeadView.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: UILayoutPriority.defaultHigh, verticalFittingPriority:UILayoutPriority.fittingSizeLevel );
//set
self.empyHeadView.bounds = CGRect(origin: CGPoint.zero, size: resultSize)
self.tableView.tableHeaderView = self.empyHeadView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment