I have a UITableView
's tableHeaderView
property that contains an UIImage
, a single-line UILabel
and then a multi-line UILabel
. The constraints are setup as follows:
self.addConstraints([
// App icon constraints
NSLayoutConstraint(item: self.image, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 8),
NSLayoutConstraint(item: self.image, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0),
NSLayoutConstraint(item: self.image, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 128),
NSLayoutConstraint(item: self.image, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 128),
// App name label constraints
NSLayoutConstraint(item: self.appNameLabel, attribute: .Top, relatedBy: .Equal, toItem: self.image, attribute: .Bottom, multiplier: 1, constant: 8),
NSLayoutConstraint(item: self.appNameLabel, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 16),
NSLayoutConstraint(item: self.appNameLabel, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: -16),
// Description label constrains
NSLayoutConstraint(item: self.descriptionLabel, attribute: .Top, relatedBy: .Equal, toItem: self.appNameLabel, attribute: .Bottom, multiplier: 1, constant: 8),
NSLayoutConstraint(item: self.descriptionLabel, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 16),
NSLayoutConstraint(item: self.descriptionLabel, attribute: .Trailing, relatedBy: .Equal, toItem: self, attribute: .Trailing, multiplier: 1, constant: -16),
NSLayoutConstraint(item: self.descriptionLabel, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: -8)
])
This produces the following:
I can rotate the device and the label will resize to the correct size thanks to the following code:
override func layoutSubviews() {
self.updateDescriptionLabelPMLW()
super.layoutSubviews()
}
private func updateDescriptionLabelPMLW() {
// -32 for the 16 padding on the left and right
let newValue = self.frame.width - 32
if self.descriptionLabel.preferredMaxLayoutWidth != newValue {
self.descriptionLabel.preferredMaxLayoutWidth = newValue
self.descriptionLabel.sizeToFit()
}
}
Despite this working on iOS 7.0-9.0 (which is only slightly short of a miracle), when I use the Slide Over -> Split View feature of the iPad Air 2 (simulator) the label is truncated to a single line when the slid-over app is made in the "split" app, as seen in the following screenshot:
Note that this only happens in landscape, and can be "fixed" by rotating the device.