簡體   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