0

制約を使用してレイアウトを作成しようとしていますが、内部にネストされたサブビューを持つ多くのカスタムコントロールがあります。また、トップビュー(CustomView)に制約を追加していますが、サブビューが適切にレイアウトされていません。

元。UITextFieldの上下にラベルを表示するTextFieldWithLabelクラスがあり、TextFieldWithLabelのインスタンスを作成し、制約付きのスーパービューに追加しています。

しかし、期待どおりの結果が表示されていません。表示されていますが、必要な場所に配置されていません。このため、TextFieldWithLabelクラス全体をAutoLayoutに変更したくありません。

助けてください!

usernameTextField = [[TextFieldWithLabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50) name:@"UserName"];
    [usernameTextField setTranslatesAutoresizingMaskIntoConstraints:NO];
    [superview addSubview:usernameTextField];
    NSLayoutConstraint* myConstraint = [NSLayoutConstraint constraintWithItem:usernameTextField
                                                                    attribute:NSLayoutAttributeCenterY
                                                                    relatedBy:NSLayoutRelationEqual
                                                                       toItem:superview
                                                                    attribute:NSLayoutAttributeCenterY
                                                                   multiplier:1
                                                                     constant:0];

    [superview addConstraint:myConstraint];

    myConstraint = [NSLayoutConstraint constraintWithItem:usernameTextField
                                                attribute:NSLayoutAttributeCenterX
                                                relatedBy:NSLayoutRelationEqual
                                                   toItem:superview
                                                attribute:NSLayoutAttributeCenterX
                                               multiplier:1
                                                 constant:0];
    [superview addConstraint:myConstraint];

スクリーンショット:ここでは中央に配置されておらず、テキストフィールド(RedColor)もクリックできません。ラベルとテキストフィールドはTextFieldWithLabel内に配置されています。注意:TextFieldWithLabelはComponentViewのサブクラスであり、ComponentViewはUIViewのサブクラスです。したがって、これが問題である可能性がありますか?ComponentsViewも自動レイアウトする必要がありますか? UITextField

4

3 に答える 3

3

自動レイアウトでは、フレームサイズは無視されます(で使用するものinitWithFrame:)。配置の制約を指定しましたが、サイズとは関係ありません。したがって、TextFieldWithLabelサイズはゼロで、画面の中央に配置されます。

このコントロールの内部で自動レイアウトを使用していないと言うので、テキストフィールドとラベルは引き続き表示されます。おそらく、その中にいくつかのフレームと自動サイズ変更マスクを設定します。したがって、テキストフィールドとラベルはの境界外にあるTextFieldWithLabelため、表示されますが、タッチに応答しない可能性があります。

この問題を解決するには、次の2つのオプションがあります。

  • 内部で自動レイアウトを使用します。たとえば、(VFLで)この制約がある場合、コントロールに高さが自動的に与えられます。"V:|-[label]-[textField]-|"
  • 上記のコードにサイズ制限を追加します-initWithFrameで行ったのと同じサイズを使用します。高さを設定するためのものは次のとおりです。

    [NSLayoutConstraint constraintWithItem:usernameTextField 
                                 attribute:NSLayoutAttributeHeight 
                                 relatedBy:NSLayoutRelationEqual 
                                    toItem:nil 
                                 attribute:NSLayoutAttributeNotAnAttribute 
                                multiplier:0.0 
                                  constant:50.0];
    

ここに、一般的な制約の作成を簡素化するためのUIViewのカテゴリがあります。あなたがするカテゴリコードを使用しcenterInView:constrainToSize:

于 2013-03-28T08:34:20.943 に答える
0

ラベル(または他のサブビュー)にサイズ変更の制約を持たせたくない場合は...

    self.YourLabel.translatesAutoresizingMaskIntoConstraints = NO;
于 2013-03-26T13:54:23.113 に答える
0

そのTextFieldWithLabelをどのように処理するかわからない場合、それが実際にUIViewサブクラスであるかどうかはわかりません(私は常にViewで終わるUIViewサブクラスに名前を付けます)

その場合、アプリを作成するときに間に入る卑劣なビューになってしまいました。InterfaceBuilderで、ビューをドラッグし、おそらくクラスを「TextFieldWithLabel_View_」に設定しまし通常の「カスタム制約」を実行しましたが、「TextFieldWithLabel_View_ 」クラスがUIViewからサブクラス化されている場合は、中間レイヤーを導入しました。

ラベルとテキストフィールドをレイアウトするXIBまたはコードでは、おそらく、独自のサブクラスのUIViewに対して適切な制約を設定しているはずです。

問題は、サブクラス化されたUIViewの制約が、StoryBoardでドラッグされたビューにリンクされていないことです。

これが私がしたことです:

@interface TextFieldWithLabel__View__ ()
@property (strong, nonatomic) IBOutlet UIView *backgroundView;
@property (weak, nonatomic) IBOutlet UILabel *labelText;
....
....
@end



-(id)initWithCoder:(NSCoder *)decoder{
    if ((self = [super initWithCoder:decoder])){
        [self addSubview:[[[NSBundle mainBundle] loadNibNamed:@"TextFieldWithLabel__View__" owner:self options:nil] objectAtIndex:0]];
    }

    // Set the backgroundView (the one needed for the IB to create a UIView for XIB)
    // Left, Right, Top and Bottom to 0

    _backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_backgroundView
                                                     attribute:NSLayoutAttributeLeft
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self
                                                     attribute:NSLayoutAttributeLeft
                                                    multiplier:0
                                                      constant:0
                         ]
     ];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_backgroundView
                                                     attribute:NSLayoutAttributeRight
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self
                                                     attribute:NSLayoutAttributeRight
                                                    multiplier:1
                                                      constant:0
                         ]
     ];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_backgroundView
                                                     attribute:NSLayoutAttributeTop
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self
                                                     attribute:NSLayoutAttributeTop
                                                    multiplier:0
                                                      constant:0
                         ]
     ];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_backgroundView
                                                     attribute:NSLayoutAttributeBottom
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self
                                                     attribute:NSLayoutAttributeBottom
                                                    multiplier:1
                                                      constant:0
                         ]
     ];
    return self;
}

これで、CustomViewを再認識したり、アイテムをドラッグしたり、割り当てられた領域のサイズを変更したりする瞬間に、CustomViewの制約フォームをTextFieldWithLabel_View_にうまく渡すことができます

ストーリーボードの「AutoResizeSubviews」にチェックマークを付けます

お役に立てば幸いです。

PS AutoLayoutをあきらめないでください...マスターしてください!

于 2013-03-27T15:38:36.553 に答える