在 Xcode 的调试控制台输出中禁用自动布局约束错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31239934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:25:35  来源:igfitidea点击:

Disable autolayout constraint error messages in debug console output in Xcode

iosxcodeautolayout

提问by Markus Rautopuro

Is there a way to (temporarily) disable autolayout error/warning messages:

有没有办法(暂时)禁用自动布局错误/警告消息:

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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x170098420 H:[UIView:0x15c62c100(2)]>",
    "<NSLayoutConstraint:0x170098600 UIView:0x15c6294f0.leading == UIView:0x15c628d40.leadingMargin - 8>",
    "<NSLayoutConstraint:0x170098650 H:[UIView:0x15c6294f0]-(0)-[UIView:0x15c62b750]>",
    "<NSLayoutConstraint:0x1700986a0 UIView:0x15c62b750.width == UIView:0x15c6294f0.width>",
    "<NSLayoutConstraint:0x1700986f0 UIView:0x15c628d40.trailingMargin == UIView:0x15c62b750.trailing - 8>",
    "<NSLayoutConstraint:0x170098880 H:[UIView:0x15c6294f0]-(0)-[UIView:0x15c62c100]>",
    "<NSLayoutConstraint:0x1700988d0 UIView:0x15c628d40.centerX == UIView:0x15c62c100.centerX + 1>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170098420 H:[UIView:0x15c62c100(2)]>

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.

回答by Witold Skibniewski

Did some decompiling and there's actually a way:

做了一些反编译,实际上有一种方法:

for Swift 5

Swift 5

UserDefaults.standard.set(false, forKey: "_UIConstraintBasedLayoutLogUnsatisfiable")

Objective-C

目标-C

[[NSUserDefaults standardUserDefaults] setValue:@(NO) forKey:@"_UIConstraintBasedLayoutLogUnsatisfiable"];

Now you'll only get notified that "_UIConstraintBasedLayoutLogUnsatisfiable is OFF".

现在,您只会收到“_UIConstraintBasedLayoutLogUnsatisfiable 已关闭”的通知。

Obligatory reminder for anyone reading this: this should be last resort, for tips on debugging and fixing constraint issues see WWDC's "Mysteries of Auto Layout" sessions: part 1and part 2.

对阅读本文的任何人的强制性提醒:这应该是最后的手段,有关调试和修复约束问题的提示,请参阅 WWDC 的“自动布局之谜”会议:第 1部分第 2 部分

回答by Sourabh Sharma

Swift 4.0 & Swift 5.0 Solution:

Swift 4.0 & Swift 5.0 解决方案:

//Hide Autolayout Warning
UserDefaults.standard.set(false, forKey: "_UIConstraintBasedLayoutLogUnsatisfiable")

Swift 3.0 Solution:

Swift 3.0 解决方案:

//Hide Autolayout Warning
UserDefaults.standard.setValue(false, forKey:"_UIConstraintBasedLayoutLogUnsatisfiable")

回答by Charlton Provatas

for anyone who is wondering how to do this macOS / osx with AppKit/Cocoa

对于想知道如何使用 AppKit/Cocoa 执行此 macOS / osx 的任何人

UserDefaults.standard.set(false, forKey: "NSConstraintBasedLayoutLogUnsatisfiable")
UserDefaults.standard.set(false, forKey: "__NSConstraintBasedLayoutLogUnsatisfiable")

回答by danylokos

One more option is to temporarily put -_UIConstraintBasedLayoutLogUnsatisfiable NO(notice the dash) into Arguments Passed On Launchmenu, under the Target -> Edit Scheme... -> Run -> Argumentstab, like this:

另一种选择是暂时将-_UIConstraintBasedLayoutLogUnsatisfiable NO(注意破折号)放入Arguments Passed On Launch菜单中的Target -> Edit Scheme... -> Run -> Arguments选项卡下,如下所示:

enter image description here

在此处输入图片说明

回答by Palle

I have tried to solve this using lldb and I found a solution:

我试图使用 lldb 解决这个问题,我找到了一个解决方案:

  1. Create a Symbolic Breakpoint for the Symbol UIViewAlertForUnsatisfiableConstraints in the Module UIKit.
  2. As breakpoint action add the debugger command "thread return"
  3. Add a second debugger command: "c"
  4. Disable "Automatically continue after evaluating actions"
  1. 为模块 UIKit 中的符号 UIViewAlertForUnsatisfiableConstraints 创建一个符号断点。
  2. 作为断点操作添加调试器命令“线程返回”
  3. 添加第二个调试器命令:“c”
  4. 禁用“评估操作后自动继续”

If your App encounters a layout issue, the execution is paused. With the instruction "thread return", the UIViewAlertForUnsatisfiableConstraints function which prints out the error is forced to return before the warning message is printed into the console.

With the "c" command the execution of your app is continued (Note: "Automatically continue after evaluating actions" does somehow not work in this case)

However with these automatic actions the breakpoint is likely to behave strangely if it gets hit two times in a row which will cause your app to crash. To solve this you can remove the breakpoint actions and enter the commands manually when the debugger pauses the execution of the program.

如果您的 App 遇到布局问题,则执行会暂停。使用“线程返回”指令,打印出错误的 UIViewAlertForUnsatisfiableConstraints 函数在警告消息打印到控制台之前被迫返回。

使用“c”命令继续执行您的应用程序(注意:“在评估操作后自动继续”在这种情况下以某种方式不起作用)

但是,使用这些自动操作,如果断点被击中两次,则可能会表现得很奇怪一行会导致您的应用程序崩溃。要解决此问题,您可以删除断点操作并在调试器暂停程序执行时手动输入命令。