模拟器和设备中的Autolayout不同

时间:2017-10-31 18:31:25

标签: ios swift uitableview autolayout ios-simulator

我正构建一个带autolayout的UITableViewCells。它在模拟器中按预期工作,没有自动布局错误或警告。

在设备上运行应用程序(与模拟器相同的类型)会显示以下日志消息,并且布局不符合预期。

2017-10-31 18:59:02.724396+0100 StaticTableTest01[8239:4373602] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(
 "<NSLayoutConstraint:0x170099050 V:|-(10)-[UILabel:0x100e0bb90'row 0 sec: 0']   (active, names: '|':UITableViewCellContentView:0x100e06e40 )>",
 "<NSLayoutConstraint:0x170099230 UITextField:0x100f083b0.top == UILabel:0x100e0bb90'row 0 sec: 0'.top   (active)>",
 "<NSLayoutConstraint:0x170099320 UITextField:0x100f083b0.height == 41   (active)>",
 "<NSLayoutConstraint:0x1700993c0 UITextField:0x100f083b0.bottom == UITableViewCellContentView:0x100e06e40.bottom - 10   (active)>",
 "<NSLayoutConstraint:0x174093920 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x100e06e40.height == 43.5   (active)>"
)

造成这种差异的原因是什么?

这是UITableViewCell的代码

let cell = UITableViewCell()
cell.translatesAutoresizingMaskIntoConstraints = false

let lb1 = UILabel()
lb1.translatesAutoresizingMaskIntoConstraints = false
lb1.lineBreakMode = .byCharWrapping
lb1.numberOfLines = 0

if (indexPath.section == 1 && indexPath.row == 0)
{ lb1.text = "r\(indexPath.row)c:\(indexPath.section) very long text very long text very long text very long text very long text very long text very long text very long text very long text"
} else
{ lb1.text = "row \(indexPath.row) sec: \(indexPath.section)"
}


let tf1 = UITextField()
tf1.font = UIFont.boldSystemFont(ofSize: 30)
tf1.translatesAutoresizingMaskIntoConstraints = false

cell.contentView.addSubview(lb1)
cell.contentView.addSubview(tf1)

let second = lb1.superview

NSLayoutConstraint(item: lb1, attribute: .leading, relatedBy: .equal, toItem: second, attribute: .leading, multiplier: 1, constant: 10).isActive = true
NSLayoutConstraint(item: lb1, attribute: .top, relatedBy: .equal, toItem: second, attribute: .top, multiplier: 1, constant: 10).isActive = true
NSLayoutConstraint(item: lb1, attribute: .width, relatedBy: .equal, toItem:nil , attribute: .notAnAttribute , multiplier: 1, constant: 250).isActive = true

tf1.leadingAnchor.constraint(equalTo: lb1.trailingAnchor, constant: 10).isActive = true
tf1.trailingAnchor.constraint(equalTo: (second?.trailingAnchor)!, constant: -10).isActive = true
tf1.topAnchor.constraint(equalTo: lb1.topAnchor, constant: 0).isActive = true

let labelHeight = lb1.textRect(forBounds: CGRect(x:0, y:0, width:250, height:1000), limitedToNumberOfLines: 4).size.height
let textFieldHeight = tf1.intrinsicContentSize.height

tf1.heightAnchor.constraint(equalToConstant: textfHeight).isActive = true

if labelHeight >= textfHeight
{ NSLayoutConstraint(item: lb1, attribute: .bottom, relatedBy: .equal, toItem: second, attribute: .bottom, multiplier: 1, constant: -10).isActive = true
} else
{ NSLayoutConstraint(item: tf1, attribute: .bottom, relatedBy: .equal, toItem: second, attribute: .bottom, multiplier: 1, constant: -10).isActive = true
}

Screenshot Simulator Screenshot Device

1 个答案:

答案 0 :(得分:1)

删除在文本字段中设置高度约束的行:

let labelHeight = lb1.textRect(forBounds: CGRect(x:0, y:0, width:250, height:1000), limitedToNumberOfLines: 4).size.height
let textFieldHeight = tf1.intrinsicContentSize.height

// delete this line
//tf1.heightAnchor.constraint(equalToConstant: textFieldHeight).isActive = true

if labelHeight >= textFieldHeight
{ NSLayoutConstraint(item: lb1, attribute: .bottom, relatedBy: .equal, toItem: second, attribute: .bottom, multiplier: 1, constant: -10).isActive = true
} else
{ NSLayoutConstraint(item: tf1, attribute: .bottom, relatedBy: .equal, toItem: second, attribute: .bottom, multiplier: 1, constant: -10).isActive = true
}

由于您要求 .intrinsicContentSize.height,因此尝试设置高度并没有多大意义。