2

コントローラーに次の簡単なテスト コードがあります。

- (void)loadView
{
    UIView *view = [UIView new];
    [self setView:view];

    UILabel *label = [UILabel new];
    [label setText:@"Hello World!"];

    [view addSubview:label];

    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[label]"
        options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];
} 

コードは次の例外で失敗し、その理由がわかりません。どんな助けでも大歓迎です:

2013-04-15 14:15:47.880 libmarkup-test[1072:c07] 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:0x753eb60 H:|-(NSSpace(20))-[UILabel:0x7536b60]   (Names: '|':UIView:0x75376a0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x712a2c0 h=--& v=--& UILabel:0x7536b60.midX ==>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x753eb60 H:|-(NSSpace(20))-[UILabel:0x7536b60]   (Names: '|':UIView:0x75376a0 )>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

「制約を同時に満たすことができません」というメッセージは、私が知る限り、制約を 1 つしか指定していないため、特に混乱を招きます。

4

2 に答える 2

8

translatesAutoresizingMaskIntoConstraintsUILabel の設定を忘れているようです。デフォルトでは YES になります。したがって、そのラベルの自動サイズ変更マスクは追加の制約に変換され、指定したものと競合します。

これを追加すると、制約の問題が修正されます。

label.translatesAutoresizingMaskIntoConstraints = NO;

おそらく、そのラベルの垂直方向の制約についても考える必要があります。

于 2013-04-15T19:34:40.840 に答える
-1

制約の優先度 (デフォルトは 1000) とハグの優先度 (デフォルトは 250) を調整してください。

于 2018-09-17T07:07:56.430 に答える