【发布时间】:2024-01-14 07:23:01
【问题描述】:
我有一个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(模拟器)时,当在“拆分”应用,如以下屏幕截图所示:
请注意,这只发生在横向,并且可以通过旋转设备来“修复”。
【问题讨论】:
标签: ios uitableview uikit ios9