Ios 为什么在模拟器中的UI布局正常时调试视图层次结构时会出现这些布局问题
我使用可视化格式在单元格中布局子视图,如下所示:Ios 为什么在模拟器中的UI布局正常时调试视图层次结构时会出现这些布局问题,ios,debugging,ios-autolayout,Ios,Debugging,Ios Autolayout,我使用可视化格式在单元格中布局子视图,如下所示: contentView.addSubview(accountLabel) contentView.addSubview(contentLabel) contentView.addSubview(timeLabel) let views: [String : Any] = [ "accountLabel": accountLabel, "contentLabel": contentLabel, "timeLabel": t
contentView.addSubview(accountLabel)
contentView.addSubview(contentLabel)
contentView.addSubview(timeLabel)
let views: [String : Any] = [
"accountLabel": accountLabel,
"contentLabel": contentLabel,
"timeLabel": timeLabel
]
let hConstraint1 = NSLayoutConstraint.constraints(withVisualFormat: "H:|-15-[accountLabel]-[timeLabel]-8-|", options: [.alignAllCenterY], metrics: nil, views: views)
let hConstraint2 = NSLayoutConstraint.constraints(withVisualFormat: "H:[contentLabel]-8-|", options: [], metrics: nil, views: views)
let vConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|-12-[accountLabel]-8-[contentLabel]-12-|", options: [.alignAllLeading], metrics: nil, views: views)
NSLayoutConstraint.activate(hConstraint1+hConstraint2+vConstraint)
在模拟器中运行时,它看起来正常:
但是,调试视图层次结构时会出现一些布局问题:
为什么运行时UI看起来正常时会出现这些布局问题
上面的视觉格式字符串是否有问题?还是Xcode的错误
如何消除这些布局问题?嗯,我不是约束方面的专家,但我确实看到了一个“可纠正”的问题和一个“很奇怪,可能是个bug”的问题 据我所知,auto layout将尝试满足约束——即使它们是不明确的。如果失败,则您会在控制台中看到错误消息,通常是“将尝试通过中断来满足…” 因此,您在调试视图层次结构中看到的警告信息比“修复此错误”指令更具信息性 也就是说
您的
accountLabel
和timeLabel
相互约束,但它们都具有相同的压缩阻力
。因此,只要每个标签中的文本量“合适”,一切都可以
但是,假设accountLabel
获取一个文本字符串“此用户名太长,不能放在这里”
如果它看起来像这样(我使用背景色来帮助查看帧):
或者像这样:
contentView.addSubview(accountLabel)
contentView.addSubview(contentLabel)
contentView.addSubview(timeLabel)
let views: [String : Any] = [
"accountLabel": accountLabel,
"contentLabel": contentLabel,
"timeLabel": timeLabel
]
let hConstraint1 = NSLayoutConstraint.constraints(withVisualFormat: "H:|-15-[accountLabel]-[timeLabel]-8-|", options: [.alignAllCenterY], metrics: nil, views: views)
let hConstraint2 = NSLayoutConstraint.constraints(withVisualFormat: "H:[contentLabel]-8-|", options: [], metrics: nil, views: views)
let vConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|-12-[accountLabel]-8-[contentLabel]-12-|", options: [.alignAllLeading], metrics: nil, views: views)
NSLayoutConstraint.activate(hConstraint1+hConstraint2+vConstraint)
这就是为什么水平位置和宽度显示为“不明确”
您可以通过添加一行来解决该问题。假设要截断accountLabel
:
accountLabel.setContentCompressionResistancePriority(749, for: .horizontal)
我会成功的。默认值为750
,因此该行告诉auto layout为timeLabel
提供更高的电阻
显示为模糊的高度和垂直位置似乎不正确。我看不出约束有什么问题 如果我对所有三个标签使用相同的字体否不明确的警告 如果我对
timeLabel
使用不同的字体大小(如您在图像中所示),则调试视图层次结构中会出现警告
除了…如果我滚动以重复使用单元格?警告已经消失了
编辑:正如OP在其评论中所述,可以通过为accountLabel
和contentLabel
赋予不同的内容优先权
值来消除垂直歧义(在滚动之前)
由于默认的拥抱
优先级为250
:
accountLabel.setContentHuggingPriority(249, for: .vertical)
清除该警告
我不认为这是必要的。。。但它似乎确实起到了作用。您在使用情节提要吗?@user1000控制器和tableView是在情节提要中创建的。单元格是由代码创建的。是否使用
tableView.rowHeight=UITableViewAutomaticDimension
?顺便说一句-请不要张贴你的代码的屏幕上限。。。发布代码本身(这样,如果有人想要复制您的条件,我们可以复制/粘贴它)。@DonMag是的,我使用UITableViewAutomaticDimension
。有什么问题吗?顺便说一句,谢谢你的建议。@DonMag我已经用代码更新了我的问题。@DonMag按照你的想法,我将contentLabel
的垂直contentHuggingPriority
更改为高于默认值250。然后,警告就消失了!实际上,当contentLabel
和accountLable
的垂直contentHuggingPriority
不同时,警告就会消失。你可以更新你的答案,我会接受的。@a_-tuo-我当然认为我试过了。。。但显然不是,因为它确实起到了作用。我更新了我的答案。