Tom Kraina Tom Kraina - 1 year ago 125
iOS Question

How to debug layout with Multiline UILabel / autolayout in notification content extension

How to debug the following issue? Is there a way how to work around this issue?

There seems to be a bug in iOS 10.2 and below when laying out a multi-line UILabel.

I have a fairly simple UIView subclass which I use in both app and notification content extension, that looks like this:

custom UIView subclass in interface builder

In the main app, everything is laid out just fine:

correct layout when shown in the main app

When shown in notification content extension on iOS 10.2 and below, the layout is broken. But only when the text is long enough to be broken into multiple lines. Seems like iOS can't calculate correct height of the whole view:

broken layout in iOS 10.2

However, this issue seems to be fixed on iOS 10.3 and newer:

correct layout in iOS 10.3

Answer Source

I started experimenting with the subviews, specifically by setting fixed height constraints.

Turns out, it was not the label(s) that caused the issue with calculating overall height but the aspect ratio constraint (width:height) on the topmost view.

Programmatically calculating height based on the view's width and setting a height constraint for the affected view helped to fix the issue:

public override func updateConstraints() {
    super.updateConstraints()

    if #available(iOS 10.2, *) {
        imageContainerHeightConstraint.isActive = false
    } else {
        // FIX: multiline label / aspect ratio / autolayout bug in iOS < 10.2
        let ratio: CGFloat = imageContainerAspectRatioConstraint.multiplier
        imageContainerHeightConstraint.constant = round(bounds.width/ratio)
        imageContainerHeightConstraint.isActive = true
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download