됩니다. loadView 메서드를 사용하여 프로그래밍 방식으로 뷰를 만듭니다. UIViewController에자동 레이아웃은 예외
의헤더
@property (weak, nonatomic) UITextField *textField;
구현이 예외가 발생 실행
-(void)loadView{
//Frame
UIView *frame = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
[self setView:frame];
[self.view setBackgroundColor:[UIColor redColor]];
//TextField
[self.textField setDelegate:self];
[self.textField setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.textField setBackgroundColor:[UIColor blueColor]];
[frame addSubview:self.textField];
[frame addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-([email protected])-[textField([email protected])]-([email protected])-|"
options:0
metrics:nil
views:@{@"textField" : self.textField}]];
[frame addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: @"V:|-([email protected])-[textField([email protected])]-([email protected])-|"
options:0
metrics:nil
views:@{@"textField" : self.textField}]];
// //Test with UIView instead
// UIView *sub = [[UIView alloc]init];
// [sub setTranslatesAutoresizingMaskIntoConstraints:NO];
// [sub setBackgroundColor:[UIColor greenColor]];
// [frame addSubview:sub];
//
// [frame addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-([email protected])-[sub([email protected])]-([email protected])-|"
// options:0
// metrics:nil
// views:@{@"sub" : sub}]];
//
// [frame addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: @"V:|-([email protected])-[sub([email protected])]-([email protected])-|"
// options:0
// metrics:nil
// views:@{@"sub" : sub}]];
. 흥미롭게도 UIView ("sub") 대신 하위보기 (주석 처리 된 부분)를 사용하면 예상대로 작동합니다.
OUPUT :
가 텍스트 필드 그냥이 UIView의처럼 행동하지 않나요? 어떻게 textField가 예외를 발생 시키나요?
새로운 발견 : 나는 self.textField에 addConstraints을 사용할 때 응용 프로그램은 충돌합니다. 이 두 번의 호출을 제거하면 충돌을 방지 할 수 있지만 텍스트 필드는 나타나지 않습니다.
예외는 무엇입니까? – rdelmar
AppDelegate의 SIGABRT에 다음 줄이 있습니다. "return UIApplicationMain (argc, argv, nil, NSStringFromClass ([AppDelegate class]));" 오류 로그에서 더 구체적인 것을 찾을 수 없습니다. – Bernd
textField는 어디에 생성합니까? 코드로 작성합니까, 아니면 IB에서 작성한 텍스트 필드의 IBOutlet입니까? init의 텍스트 필드를 코드의 첫 번째 행으로 할당하면 더 이상 충돌이 발생하지 않습니다 (제약 조건은 제대로 작동하지 않지만 적어도 충돌은 발생하지 않습니다). – rdelmar