1

望ましいシナリオ: iOS6 の AutoLayout と Frames を介してカスタム アラート ビューを作成します。

1) ホスト ビュー (UIController.view) に空の UIView を作成します。

alertView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[alertView(==300)]" options:0  metrics:nil views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[alertView]-|" options:0  metrics:nil views:viewsDict]];

これは機能します: ホスト ビュー (サブビュー) にアラート UIView が表示されます。


ただし、UILabel を Alert ビューの爆弾に追加しようとしています。

UIView (1) が描画されていることを確認したので、代わりに UILabel を代入して、何かを取得できるかどうかを確認しました。

- (UILabel *)createLabelWithMessage:(NSString *)message {
    if (!message) return nil;
    UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0,0)];
    myLabel.text = message;
    [myLabel setFont:[UIFont systemFontOfSize:12.0]];
    [myLabel setAutoresizingMask:UIViewAutoresizingNone]; 
    myLabel.backgroundColor = [UIColor clearColor];
    return myLabel;
}
...
titleLabel = [self createLabelWithMessage:@"Danger"];
...
// ...replacing 'alertView' (UIView) with 'titleView' (UILabel):
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[titleLabel(==300)]" options:0  metrics:nil views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[titleLabel]-|" options:0  metrics:nil views:viewsDict]];

質問: なぜ UILabel は爆発するのに、UIView は正常に描画されているように見えるのですか?

Xcode からのヒントは次のとおりです。

AutoLayoutContraints[3828:11303] 制約を同時に満たすことができません。

おそらく、次のリストの制約の少なくとも 1 つが望ましくないものです。これを試して:

(1) 各制約を見て、どれが予期しないものかを把握してみてください。

(2) 不要な制約を追加したコードを見つけて修正します。
(注: 理解できない NSAutoresizingMaskLayoutConstraints が表示されている場合は、UIView プロパティ translatesAutoresizingMaskIntoConstraints のドキュメントを参照してください) ( "", "", "" )

制約を破って回復を試みます (名前: '|':UIView:0x128727a0 )>

私の 'Visual-Formatting' 言語を台無しにしている UILabel 内に隠しプロパティがなければなりません。

..「alertView」の別のサブビューを作成しました。同じエラーが発生します。UIControllerのビューの(サブビュー)に1つのUIView(「alertView」)を表示するだけで、「良い」結果しか得られないようです。これ以上何もない。


隠された何かが単純な制約と競合しています。そして、私は何を知りません。

ところで:「use autoLayout」を「ON」にして、NIBをホストUIViewとして使用しています。ただし、iOS6 でチェックされたルーチン内で、「自動レイアウト」(iOS 4.3 以降) を使用しないより大きなコード内でこれを使用します。

4

1 に答える 1

1

解決策:(私のコメントによると)特定のサブビューに対して「setTranslatesAutoresizingMaskIntoConstraints」フラグを「NO」に設定するのを忘れていました。スーパーコンテナのためにやったのですが。
例:
[testView setTranslatesAutoresizingMaskIntoConstraints:NO];

すべてのメンバー UIViewに「setTranslatesAutoresizingMaskIntoConstraints」使用することを忘れないでください。

于 2013-03-18T22:53:48.300 に答える