繁体   English   中英

模拟器和设备中的自动布局不同

[英]Autolayout different in Simulator and Device

我正在构建一个带有自动布局的 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
}

截图模拟器 截图设备

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

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 ,因此尝试设置高度没有多大意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM