簡體   English   中英

如何在通知內容擴展中使用多行UILabel /自動布局調試布局

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

如何調試以下問題? 有沒有一種方法可以解決此問題?

布局多行UILabel時,iOS 10.2及更低版本中似乎存在一個錯誤。

我有一個相當簡單的UIView子類,可在應用程序和通知內容擴展中使用,如下所示:

接口構建器中的定制UIView子類

在主應用程序中,一切都布置得很好:

在主應用程序中顯示時正確的布局

當在iOS 10.2和更低版本的通知內容擴展中顯示時,布局已損壞。 但是僅當文本足夠長才能分成多行時。 似乎iOS無法計算整個視圖的正確高度:

iOS 10.2中的布局已損壞

但是,此問題似乎已在iOS 10.3和更高版本上修復:

iOS 10.3中的正確布局

我開始嘗試子視圖,特別是通過設置固定的高度約束。

事實證明,不是標簽引起了計算總高度的問題,而是最頂部視圖上的寬高比約束 (寬度:高度)。

根據視圖的寬度以編程方式計算高度,並為受影響的視圖設置高度限制,有助於解決此問題:

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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM