iOS定位解决束缚冲突

摘要:前言在做项目的时候,无意间看到自动布局束缚警告,开始也也觉得没什么,尽管有警告,但并不影响UI展现效果。但是越来越有代码洁癖的我,也忍受不了控制台输出一大堆束缚警告Log,于是就查阅如何定位处理束缚冲突,同时自己也记录下来。下面开始详情具体操作步骤吧。一、增加UIViewAlertForUnsati

前言

在做项目的时候,无意间看到自动布局束缚警告,开始也也觉得没什么,尽管有警告,但并不影响UI展现效果。但是越来越有代码洁癖的我,也忍受不了控制台输出一大堆束缚警告Log,于是就查阅如何定位处理束缚冲突,同时自己也记录下来。下面开始详情具体操作步骤吧。

一、增加UIViewAlertForUnsatisfiableConstraints断点

  • 增加Symbolic Breakpoints
    屏幕快照 2018-06-04 下午2.44.35.png
  • 编辑断点symbol填入
UIViewAlertForUnsatisfiableConstraints
  • 增加控制台打印action
po [[UIWindow keyWindow] _autolayoutTrace]
屏幕快照 2018-06-04 下午2.44.50.png

二、定位束缚警告冲突

  • 增加好断点之后,当界面有束缚冲突,就会触发断点,控制打印如下:
[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. (    <MASLayoutConstraint:0x604000ab04a0 UIButton:0x7faf99f04010.width == 40>,    <MASLayoutConstraint:0x604000ab66e0 UIButton:0x7faf99f04010.left == CYBButtonView:0x7faf99f83360.left + 10>,    <MASLayoutConstraint:0x604000abaa00 UILabel:0x7faf99f5f8e0.left == UIButton:0x7faf99f04010.right>,    <MASLayoutConstraint:0x604000abd580 UILabel:0x7faf99f5f8e0.left == CYBButtonView:0x7faf99f83360.left + 15>,)Will attempt to recover by breaking constraint <MASLayoutConstraint:0x604000ab04a0 UIButton:0x7faf99f04010.width == 40>Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
  • 根据提醒,找到束缚有问题的控件地址0x7faf99f04010,而后全局搜索,就能找到具体是哪个控件
  • 假如控制台打印不够直观看出是哪个控件束缚有问题,我们可以通过 LLDB命令工具chisel定位寻觅。

三、处理冲突

通常处理冲突的方法有:

  • 删除多余束缚
  • 修改束缚优先级

参考

1、How to trap on UIViewAlertForUnsatisfiableConstraints?

  • 全部评论(0)
手机二维码手机访问领取大礼包
返回顶部