본문 바로가기
카테고리 없음

ios auto_layout 오토레이아웃 코드

by 인생여희 2019. 1. 3.
반응형

ios auto_layout 오토레이아웃 코드







#import "AutoLayoutSampleViewController.h"


@interface AutoLayoutSampleViewController ()

@property (nonatomic, strong) UIButton *leftButton;

@property (nonatomic,strong) UIButton *rightButton;

@property (nonatomic,strong) UITextField *textfield;

@end


@implementation AutoLayoutSampleViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    UIView *superview = self.view;

    

    //1. 좌 버튼 생성 + 뷰에 넣기

    self.leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.leftButton.translatesAutoresizingMaskIntoConstraints = NO;

    [self.leftButton setBackgroundColor:UIColor.greenColor];

    [self.leftButton setTitle:@"왼쪽버튼" forState:UIControlStateNormal];

    [self.view addSubview:self.leftButton];

    

    //2. 좌 버튼 제약 조건 설정 x

    NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint

                                                                                                                     constraintWithItem:self.leftButton

                                                                                                                     attribute:NSLayoutAttributeCenterX

                                                                                                                     relatedBy:NSLayoutRelationGreaterThanOrEqual

                                                                                                                     toItem:superview

                                                                                                                     attribute:NSLayoutAttributeCenterX

                                                                                                                     multiplier:1.0

                                                                                                                     constant:-60.0];

    

    //3.좌버튼 y 포지션

    

    

    NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint

                                                                                                                     constraintWithItem:self.leftButton

                                                                                                                     attribute:NSLayoutAttributeCenterY

                                                                                                                     relatedBy:NSLayoutRelationEqual

                                                                                                                     toItem:superview

                                                                                                                     attribute:NSLayoutAttributeCenterY

                                                                                                                     multiplier:1.0f

                                                                                                                     constant:0.0f];

    

    //4.슈퍼뷰에 좌버튼 추가

    [superview addConstraints:@[leftButtonXConstraint, leftButtonYConstraint]];

    

    

    /*5. 우측버튼 생성*/

    self.rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.rightButton.translatesAutoresizingMaskIntoConstraints = NO;

    [self.rightButton setBackgroundColor:UIColor.yellowColor];

    [self.rightButton setTitle:@"오른쪽버튼" forState:UIControlStateNormal];

    [self.view addSubview:self.rightButton];

    

    

    /*6. 우측버튼 X 위치 설정*/

    NSLayoutConstraint *rightButtonXConstraint = [NSLayoutConstraint

                                                                                                                      constraintWithItem:self.rightButton

                                                                                                                      attribute:NSLayoutAttributeCenterX

                                                                                                                      relatedBy:NSLayoutRelationGreaterThanOrEqual

                                                                                                                      toItem:superview

                                                                                                                      attribute:NSLayoutAttributeCenterX

                                                                                                                      multiplier:1.0

                                                                                                                      constant:60.0];

    

    /*7. 우측버튼 Y 위치 설정*/

    rightButtonXConstraint.priority = UILayoutPriorityDefaultHigh;

    NSLayoutConstraint *centerYMyConstraint = [NSLayoutConstraint

                                                                                                                   constraintWithItem:self.rightButton

                                                                                                                   attribute:NSLayoutAttributeCenterY

                                                                                                                   relatedBy:NSLayoutRelationGreaterThanOrEqual

                                                                                                                   toItem:superview

                                                                                                                   attribute:NSLayoutAttributeCenterY

                                                                                                                   multiplier:1.0

                                                                                                                   constant:0.0];

    

    [superview addConstraints:@[centerYMyConstraint,rightButtonXConstraint]];

    

    

    

    //8. 택스트 필드 생성 및 초기화

    self.textfield = [[UITextField alloc]initWithFrame:CGRectMake(0, 100, 100, 30)];

    self.textfield.borderStyle = UITextBorderStyleRoundedRect;

    self.textfield.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:self.textfield];

    

    

    

    //탑 제약 조건 -superview 에 맞췄음

    NSLayoutConstraint *textFieldTopConstraint = [NSLayoutConstraint

                                                                                                                      constraintWithItem:self.textfield

                                                                                                                      attribute:NSLayoutAttributeTop

                                                                                                                      relatedBy:NSLayoutRelationGreaterThanOrEqual

                                                                                                                      toItem:superview

                                                                                                                      attribute:NSLayoutAttributeTop

                                                                                                                      multiplier:1.0

                                                                                                                      constant:60.0];

    //아래 제약 조건 - self.rightButton의 탑에 맞췄음

    NSLayoutConstraint *textFieldBottomConstraint = [NSLayoutConstraint

                                                                                                                          constraintWithItem:self.textfield

                                                                                                                          attribute:NSLayoutAttributeTop

                                                                                                                          relatedBy:NSLayoutRelationGreaterThanOrEqual

                                                                                                                          toItem:self.rightButton

                                                                                                                          attribute:NSLayoutAttributeTop

                                                                                                                          multiplier:0.8

                                                                                                                          constant: -60.0];

    

    

    //왼쪽 제약 조건 - superview의 왼쪽에 맞췄음

    NSLayoutConstraint *textFieldLeftConstraint = [NSLayoutConstraint

                                                                                                                     constraintWithItem:self.textfield

                                                                                                                     attribute:NSLayoutAttributeLeft

                                                                                                                     relatedBy:NSLayoutRelationEqual

                                                                                                                     toItem:superview

                                                                                                                     attribute:NSLayoutAttributeLeft

                                                                                                                     multiplier:1.0

                                                                                                                     constant: 30.0];

    

    //오른쪽 제약 조건 - superview의 오른쪽에 맞췄음

    NSLayoutConstraint *textFieldRightConstraint = [NSLayoutConstraint

                                                                                                                   constraintWithItem:self.textfield

                                                                                                                   attribute:NSLayoutAttributeRight

                                                                                                                   relatedBy:NSLayoutRelationEqual

                                                                                                                   toItem:superview

                                                                                                                   attribute:NSLayoutAttributeRight

                                                                                                                   multiplier:1.0

                                                                                                                   constant: -30.0];

    

    

    [superview addConstraints:@[textFieldBottomConstraint,textFieldLeftConstraint,textFieldRightConstraint, textFieldTopConstraint]];

    

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}




@end




반응형

댓글