【问题标题】:Mutli-line UILabel truncating to 1 line when using Slide Over on iPad在 iPad 上使用 Slide Over 时多行 UILabel 截断为 1 行
【发布时间】:2024-01-14 07:23:01
【问题描述】:

我有一个UITableViewtableHeaderView 属性,其中包含一个UIImage、一个单行UILabel,然后是一个多行UILabel。约束设置如下:

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)
    ])

这会产生以下内容:

由于以下代码,我可以旋转设备,标签将调整为正确的大小:

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

尽管这在 iOS 7.0-9.0 上工作(这只是一个奇迹),但当我使用 Slide Over -> Split View feature of the iPad Air 2(模拟器)时,当在“拆分”应用,如以下屏幕截图所示:

请注意,这只发生在横向,并且可以通过旋转设备来“修复”。

【问题讨论】:

    标签: ios uitableview uikit ios9


    【解决方案1】:

    通过将updateDescriptionLabelPMLW 方法更新为:

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

    它可能并不完美,但它已经修复了错误!

    【讨论】:

      最近更新 更多