1. ホーム
  2. ios

[解決済み] Xcodeのデバッグコンソール出力で自動レイアウト制約のエラーメッセージを無効にする

2023-01-13 23:08:36

質問

autolayoutのエラー/警告メッセージを(一時的に)無効にする方法はありますか。

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.

どのように解決するのですか?

デコンパイルしてみると、実は方法があるんです。

Swift 5の場合

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

Objective-C

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

これで "_UIConstraintBasedLayoutLogUnsatisfiable is OFF" とだけ通知されるようになりました。

制約の問題をデバッグおよび修正するためのヒントについては、WWDC の "Mysteries of Auto Layout" セッションを参照してください。 パート 1 および パート 2 .