制約を使用してレイアウトを作成しようとしていますが、内部にネストされたサブビューを持つ多くのカスタムコントロールがあります。また、トップビュー(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も自動レイアウトする必要がありますか?