在 iPad 上使用 Slide Over 时多行 UILabel 截断为 1 行
Mutli-line UILabel truncating to 1 line when using Slide Over on iPad
我有一个 UITableView
的 tableHeaderView
属性,其中包含一个 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(模拟器)时,标签被截断为一行侧滑应用是在"split"应用中制作的,如下截图所示:
请注意,这只发生在风景中,可以通过旋转设备"fixed"。
通过将 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()
}
}
}
}
它可能不完美,但它修复了错误!
我有一个 UITableView
的 tableHeaderView
属性,其中包含一个 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(模拟器)时,标签被截断为一行侧滑应用是在"split"应用中制作的,如下截图所示:
请注意,这只发生在风景中,可以通过旋转设备"fixed"。
通过将 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()
}
}
}
}
它可能不完美,但它修复了错误!