매우 기본적인 원보기를 만들었습니다. 레이어 반지름을 너비의 절반으로 설정하는 사용자 지정 Init 메서드를 호출하기 만하면됩니다. View Controller는 하위 뷰를 생성하고 추가 한 다음 원 중심에 대한 제약 조건을 설정합니다.자동 레이아웃을 사용하여 프로그래밍 방식으로 사용자 화 UIView 중심 지정하기
참고 : 동일한 질문에 대해 여러 개의 SO 답변을 확인 했으므로 제약 조건이 올바르게 설정되었거나 동일하게 보입니다.
제약 조건을 추가하면보기가 표시되지 않습니다. 내가 circleView 경계와 프레임을 로그 아웃하고 그들은 프레임이 처음으로 설정되어있는 0,0,100,100으로 표시하지만보기가 전혀 표시되지 않습니까? 제약 조건을 제거하면 프레임이 0,0,100,100이고이 위치에 표시됩니다. 따라서 제약 조건을 추가하면보기가 사라집니다.
- (void)viewDidLoad
{
NSLog(@"%s",__PRETTY_FUNCTION__);
[super viewDidLoad];
[self setupViewControllerViews];
}
-(void)setupViewControllerViews {
self.circleView = [[CCZTCircleView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.view addSubview:self.circleView];
[self setupCircleViewAutoLayoutConstraints];
}
-(void)setupCircleViewAutoLayoutConstraints {
NSLayoutConstraint *xCenterConstraint = [NSLayoutConstraint constraintWithItem:self.circleView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
NSLayoutConstraint *yCenterConstraint = [NSLayoutConstraint constraintWithItem:self.circleView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
[self.view addConstraint:xCenterConstraint];
[self.view addConstraint:yCenterConstraint];
} 당신이 설정된 제한의 기초에 따라보기의 프레임을 변경합니다 자동 레이아웃 코코아 터치로, 자동 레이아웃을 사용하는 경우 처음에 설정 한 프레임은 무시됩니다
제약 조건을 추가 한 후 [self.view setNeedsDisplay]를 시도해보십시오. –
변경되지 않음 - 추가 문자 – StuartM
해당 제약 조건은 정확합니다. 문제는 일정한 너비와 높이를 설정하고 적절한 AutoresizingMask (= 18)를 설정해야한다는 것입니다. 현재 18 개가 무엇을 의미하는지 알아 내려고합니다. –