1

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:

Table header view

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:

enter image description here

Note that this only happens in landscape, and can be "fixed" by rotating the device.

1 Answer 1

0

This ended up being fixed by updating the updateDescriptionLabelPMLW method to:

private func updateDescriptionLabelPMLW() {
    /*
    iOS 7
    Tested on:
    - iPad 2 (7.0.6)
    - iPhone 4 (7.1.1)

    iOS 8.1
    Tested on:
    - iPhone 5 (sim)
    - iPad Retina (sim)

    iOS 9
    Tested on:
    - iPhone 6
    - iPad Air 2

    Does not work on iPad Air 2 when sliding in an app (label is truncated)
    */
    // -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()

        // Check for iOS 9 devices in landscape
        // This should also check for iPads (specifically iPad Air 2)
        if #available(iOS 9.0, *) {
            if UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) {
                self.superview?.setNeedsLayout()
                self.superview?.layoutIfNeeded()
            }
        }
    }
}

It may not be perfect, but it's fixed the bug!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.