ios 오토레이아웃 코드로 응용하기
.m
#import <UIKit/UIKit.h>
@interface AutoLayoutSampleViewController : UIViewController
@end
.h
#import "AutoLayoutSampleViewController.h"
@interface AutoLayoutSampleViewController ()
@property (nonatomic,strong) UILabel *bigTitle; //암호 입력
@property (nonatomic,strong) UILabel *smallTitle; //암호를 입력해주세요.
@property (nonatomic,strong) UITextField *textfield; //텍스트 필드
@property (strong, nonatomic) UIButton *one;
@property (strong, nonatomic) UIButton *two;
@property (strong, nonatomic) UIButton *three;
@property (strong, nonatomic) UIButton *four;
@property (strong, nonatomic) UIButton *five;
@property (strong, nonatomic) UIButton *six;
@property (strong, nonatomic) UIButton *seven;
@property (strong, nonatomic) UIButton *eight;
@property (strong, nonatomic) UIButton *zero;
@property (strong, nonatomic) UIButton *nine;
@property (nonatomic, strong) UIButton *clearButton;
@property (nonatomic,strong) UIButton *deleteButton;
//비번
@property(nonatomic,strong)NSString *secretKey;
@end
@implementation AutoLayoutSampleViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.lightGrayColor;
_secretKey = [[NSString alloc]init];
//최상위 제목 라벨 생성 및 초기화
self.bigTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
self.bigTitle.translatesAutoresizingMaskIntoConstraints = NO;
[self.bigTitle setFont:[UIFont boldSystemFontOfSize:24]];
self.bigTitle.text = @"암호 입력";
[self.view addSubview:self.bigTitle];
//2. 타이틀 제약 조건 설정 x
NSLayoutConstraint *bigTitleXConstraint = [NSLayoutConstraint
constraintWithItem:self.bigTitle
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0];
//3.타이틀 y 포지션
NSLayoutConstraint *bigTitleYConstraint = [NSLayoutConstraint
constraintWithItem:self.bigTitle
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:self.view.frame.size.height / 7];
//4.슈퍼뷰에 좌버튼 추가
[self.view addConstraints:@[bigTitleXConstraint, bigTitleYConstraint]];
/*중간 알림 라벨*/
//중간 알림 라벨 생성 및 초기화
self.smallTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 150, 30)];
self.smallTitle.translatesAutoresizingMaskIntoConstraints = NO;
self.smallTitle.text = @"암호를 입력해 주세요";
[self.view addSubview:self.smallTitle];
// 제약 조건 설정 x
NSLayoutConstraint *smallTitleXConstraint = [NSLayoutConstraint
constraintWithItem:self.smallTitle
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0];
//탑 제약 조건 -bigTitle 바닥 에 맞췄음
NSLayoutConstraint *smallTitleTopConstraint = [NSLayoutConstraint
constraintWithItem:self.smallTitle
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.bigTitle
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:30.0];
//슈퍼뷰에 중간 알람 라벨 추가
[self.view addConstraints:@[smallTitleXConstraint, smallTitleTopConstraint]];
/*텍스트 필드*/
// 택스트 필드 생성 및 초기화
self.textfield = [[UITextField alloc]init];
self.textfield.secureTextEntry = YES;
self.textfield.borderStyle = UITextBorderStyleRoundedRect;
self.textfield.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.textfield];
// 제약 조건 설정 x
NSLayoutConstraint *textfieldXConstraint = [NSLayoutConstraint
constraintWithItem:self.textfield
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0];
//탑 제약 조건 -smallTitle 바닥 에 맞췄음
NSLayoutConstraint *textfieldTopConstraint = [NSLayoutConstraint
constraintWithItem:self.textfield
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.smallTitle
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:30.0];
/* 가로 제약조건 */
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.textfield
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:200];
/* 높이 제약조건 */
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.textfield
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:30];
//슈퍼뷰에 텍스트 필드 추가
[self.view addConstraints:@[textfieldXConstraint, widthConstraint, heightConstraint,textfieldTopConstraint]];
/*1번 부터 - 9번 버튼*/
#pragma 전체 뷰
//전체 뷰
UIView *totalView = [[UIView alloc]init];
totalView.translatesAutoresizingMaskIntoConstraints = NO;
totalView.backgroundColor = UIColor.brownColor;
[self.view addSubview:totalView];
//3.타이틀 y 포지션
NSLayoutConstraint *totalViewTopConstraint = [NSLayoutConstraint
constraintWithItem:totalView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0];
//제약 조건 설정 좌
NSLayoutConstraint *totalViewLeftConstraint = [NSLayoutConstraint
constraintWithItem:totalView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
//제약 조건 설정 우
NSLayoutConstraint *totalViewRightConstraint = [NSLayoutConstraint
constraintWithItem:totalView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
//3.타이틀 y 포지션
NSLayoutConstraint *totalViewBottomConstraint = [NSLayoutConstraint
constraintWithItem:totalView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0];
/* 가로 제약조건 */
NSLayoutConstraint *totalViewWidthConstraint = [NSLayoutConstraint
constraintWithItem:totalView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:self.view.frame.size.width];
/* 높이 제약조건 */
NSLayoutConstraint *totalViewHeightConstraint = [NSLayoutConstraint
constraintWithItem:totalView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:self.view.frame.size.height / 2];
//4.슈퍼뷰에 좌버튼 추가
[self.view addConstraints:@[totalViewWidthConstraint, totalViewHeightConstraint,totalViewTopConstraint,totalViewLeftConstraint, totalViewRightConstraint,totalViewBottomConstraint]];
#pragma first view - !
//전체 뷰
UIView *firsthView = [[UIView alloc]init];
firsthView.translatesAutoresizingMaskIntoConstraints = NO;
firsthView.backgroundColor = UIColor.blueColor;
[totalView addSubview:firsthView];
//3. y 포지션
NSLayoutConstraint *firsthViewTopConstraint = [NSLayoutConstraint
constraintWithItem:firsthView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:totalView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *firsthViewLeftConstraint = [NSLayoutConstraint
constraintWithItem:firsthView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:totalView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *firsthViewHeightConstraint = [NSLayoutConstraint
constraintWithItem:firsthView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:totalView
attribute:NSLayoutAttributeHeight
multiplier:0.25
constant:totalView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *firsthViewWidthConstraint = [NSLayoutConstraint
constraintWithItem:firsthView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:totalView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:totalView.frame.size.width];
//토탈 뷰에 추가
[totalView addConstraints:@[firsthViewTopConstraint, firsthViewLeftConstraint,firsthViewWidthConstraint,firsthViewHeightConstraint]];
#pragma 버튼 1 - 3번
/*버튼 생성*/
self.one = [UIButton buttonWithType:UIButtonTypeSystem];
[self.one.titleLabel setFont: [self.one.titleLabel.font fontWithSize: 24]];
self.one.translatesAutoresizingMaskIntoConstraints = NO;
[self.one setBackgroundColor:UIColor.yellowColor];
[self.one setTitle:@"1" forState:UIControlStateNormal];
[self.one addTarget:self action:@selector(numButtonAction:) forControlEvents: UIControlEventTouchUpInside];
[firsthView addSubview:self.one];
//3. y 포지션
NSLayoutConstraint *oneTopConstraint = [NSLayoutConstraint
constraintWithItem:self.one
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:firsthView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *oneLeftConstraint = [NSLayoutConstraint
constraintWithItem:self.one
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:firsthView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *oneHeightConstraint = [NSLayoutConstraint
constraintWithItem:self.one
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:firsthView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:firsthView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *oneViewWidthConstraint = [NSLayoutConstraint
constraintWithItem:self.one
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:firsthView
attribute:NSLayoutAttributeWidth
multiplier:0.3333
constant:firsthView.frame.size.width];
//첫번째 뷰에 추가
[firsthView addConstraints:@[oneTopConstraint, oneLeftConstraint,oneHeightConstraint,oneViewWidthConstraint]];
//2번
/*버튼 생성*/
self.two = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.two.titleLabel setFont: [self.two.titleLabel.font fontWithSize: 24]];
self.two.translatesAutoresizingMaskIntoConstraints = NO;
[self.two setBackgroundColor:UIColor.greenColor];
[self.two setTitle:@"2" forState:UIControlStateNormal];
[self.two addTarget:self action:@selector(numButtonAction:) forControlEvents: UIControlEventTouchUpInside];
[firsthView addSubview:self.two];
//3. y 포지션
NSLayoutConstraint *twoTopConstraint = [NSLayoutConstraint
constraintWithItem:self.two
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:firsthView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *twoLeftConstraint = [NSLayoutConstraint
constraintWithItem:self.two
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.one
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *twoHeightConstraint = [NSLayoutConstraint
constraintWithItem:self.two
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:firsthView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:firsthView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *twoViewWidthConstraint = [NSLayoutConstraint
constraintWithItem:self.two
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:firsthView
attribute:NSLayoutAttributeWidth
multiplier:0.3333
constant:firsthView.frame.size.width];
//첫번째 뷰에 추가
[firsthView addConstraints:@[twoTopConstraint, twoLeftConstraint,twoHeightConstraint,twoViewWidthConstraint]];
//3번 버튼
/*버튼 생성*/
self.three = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.three.translatesAutoresizingMaskIntoConstraints = NO;
[self.three.titleLabel setFont: [self.three.titleLabel.font fontWithSize: 24]];
[self.three setBackgroundColor:UIColor.orangeColor];
[self.three setTitle:@"3" forState:UIControlStateNormal];
[self.three addTarget:self action:@selector(numButtonAction:) forControlEvents: UIControlEventTouchUpInside];
[firsthView addSubview:self.three];
//3. y 포지션
NSLayoutConstraint *threeTopConstraint = [NSLayoutConstraint
constraintWithItem:self.three
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:firsthView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *threeLeftConstraint = [NSLayoutConstraint
constraintWithItem:self.three
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.two
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *threeHeightConstraint = [NSLayoutConstraint
constraintWithItem:self.three
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:firsthView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:firsthView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *threeViewWidthConstraint = [NSLayoutConstraint
constraintWithItem:self.three
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:firsthView
attribute:NSLayoutAttributeWidth
multiplier:0.3333
constant:firsthView.frame.size.width];
//첫번째 뷰에 추가
[firsthView addConstraints:@[threeTopConstraint, threeLeftConstraint,threeHeightConstraint,threeViewWidthConstraint]];
#pragma second view - !
//전체 뷰
UIView *secondView = [[UIView alloc]init];
secondView.translatesAutoresizingMaskIntoConstraints = NO;
secondView.backgroundColor = UIColor.blueColor;
[totalView addSubview:secondView];
//탑 포지션
NSLayoutConstraint *secondViewTopConstraint = [NSLayoutConstraint
constraintWithItem:secondView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:firsthView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *secondViewLeftConstraint = [NSLayoutConstraint
constraintWithItem:secondView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:totalView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *secondViewHeightConstraint = [NSLayoutConstraint
constraintWithItem:secondView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:totalView
attribute:NSLayoutAttributeHeight
multiplier:0.25
constant:totalView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *secondViewWidthConstraint = [NSLayoutConstraint
constraintWithItem:secondView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:totalView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:totalView.frame.size.width];
//토탈 뷰에 추가
[totalView addConstraints:@[secondViewTopConstraint, secondViewLeftConstraint,secondViewHeightConstraint,secondViewWidthConstraint]];
#pragma 버튼 4 - 6번
/*버튼 생성*/
self.four = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.four.translatesAutoresizingMaskIntoConstraints = NO;
[self.four.titleLabel setFont: [self.four.titleLabel.font fontWithSize: 24]];
[self.four setBackgroundColor:UIColor.orangeColor];
[self.four setTitle:@"4" forState:UIControlStateNormal];
[self.four addTarget:self action:@selector(numButtonAction:) forControlEvents: UIControlEventTouchUpInside];
[secondView addSubview:self.four];
//3. y 포지션
NSLayoutConstraint *fourTopConstraint = [NSLayoutConstraint
constraintWithItem:self.four
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:secondView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *fourLeftConstraint = [NSLayoutConstraint
constraintWithItem:self.four
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:secondView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *fourHeightConstraint = [NSLayoutConstraint
constraintWithItem:self.four
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:secondView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:secondView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *fourViewWidthConstraint = [NSLayoutConstraint
constraintWithItem:self.four
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:secondView
attribute:NSLayoutAttributeWidth
multiplier:0.3333
constant:secondView.frame.size.width];
//두번째 뷰에 추가
[secondView addConstraints:@[fourTopConstraint, fourLeftConstraint,fourHeightConstraint,fourViewWidthConstraint]];
//5번
/*버튼 생성*/
self.five = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.five.translatesAutoresizingMaskIntoConstraints = NO;
[self.five.titleLabel setFont: [self.five.titleLabel.font fontWithSize: 24]];
[self.five setBackgroundColor:UIColor.yellowColor];
[self.five setTitle:@"5" forState:UIControlStateNormal];
[self.five addTarget:self action:@selector(numButtonAction:) forControlEvents: UIControlEventTouchUpInside];
[secondView addSubview:self.five];
//3. y 포지션
NSLayoutConstraint *fiveTopConstraint = [NSLayoutConstraint
constraintWithItem:self.five
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:secondView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *fiveLeftConstraint = [NSLayoutConstraint
constraintWithItem:self.five
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.four
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *fiveHeightConstraint = [NSLayoutConstraint
constraintWithItem:self.five
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:secondView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:secondView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *fiveViewWidthConstraint = [NSLayoutConstraint
constraintWithItem:self.five
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:secondView
attribute:NSLayoutAttributeWidth
multiplier:0.3333
constant:secondView.frame.size.width];
//두번째 뷰에 추가
[secondView addConstraints:@[fiveTopConstraint, fiveHeightConstraint,fiveLeftConstraint,fiveViewWidthConstraint]];
//6번
/*버튼 생성*/
self.six = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.six.translatesAutoresizingMaskIntoConstraints = NO;
[self.six setBackgroundColor:UIColor.greenColor];
[self.six.titleLabel setFont: [self.six.titleLabel.font fontWithSize: 24]];
[self.six setTitle:@"6" forState:UIControlStateNormal];
[self.six addTarget:self action:@selector(numButtonAction:) forControlEvents: UIControlEventTouchUpInside];
[secondView addSubview:self.six];
//3. y 포지션
NSLayoutConstraint *sixTopConstraint = [NSLayoutConstraint
constraintWithItem:self.six
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:secondView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *sixLeftConstraint = [NSLayoutConstraint
constraintWithItem:self.six
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.five
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *sixHeightConstraint = [NSLayoutConstraint
constraintWithItem:self.six
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:secondView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:secondView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *sixViewWidthConstraint = [NSLayoutConstraint
constraintWithItem:self.six
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:secondView
attribute:NSLayoutAttributeWidth
multiplier:0.3333
constant:secondView.frame.size.width];
//두번째 뷰에 추가
[secondView addConstraints:@[sixTopConstraint, sixLeftConstraint,sixHeightConstraint,sixViewWidthConstraint]];
#pragma third view - !
//전체 뷰
UIView *thirdView = [[UIView alloc]init];
thirdView.translatesAutoresizingMaskIntoConstraints = NO;
thirdView.backgroundColor = UIColor.blueColor;
[totalView addSubview:thirdView];
//3. y 포지션
NSLayoutConstraint *thirdViewTopConstraint = [NSLayoutConstraint
constraintWithItem:thirdView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:secondView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *thirdViewLeftConstraint = [NSLayoutConstraint
constraintWithItem:thirdView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:totalView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *thirdViewHeightConstraint = [NSLayoutConstraint
constraintWithItem:thirdView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:totalView
attribute:NSLayoutAttributeHeight
multiplier:0.25
constant:totalView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *thirdViewWidthConstraint = [NSLayoutConstraint
constraintWithItem:thirdView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:totalView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:totalView.frame.size.width];
//토탈 뷰에 추가
[totalView addConstraints:@[thirdViewTopConstraint, thirdViewLeftConstraint,thirdViewWidthConstraint,thirdViewHeightConstraint]];
#pragma 버튼 7 - 9번
/*버튼 생성*/
self.seven = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.seven.translatesAutoresizingMaskIntoConstraints = NO;
[self.seven setBackgroundColor:UIColor.greenColor];
[self.seven.titleLabel setFont: [self.seven.titleLabel.font fontWithSize: 24]];
[self.seven setTitle:@"7" forState:UIControlStateNormal];
[self.seven addTarget:self action:@selector(numButtonAction:) forControlEvents: UIControlEventTouchUpInside];
[thirdView addSubview:self.seven];
//3. y 포지션
NSLayoutConstraint *sevenTopConstraint = [NSLayoutConstraint
constraintWithItem:self.seven
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:thirdView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *sevenLeftConstraint = [NSLayoutConstraint
constraintWithItem:self.seven
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:thirdView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *sevenHeightConstraint = [NSLayoutConstraint
constraintWithItem:self.seven
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:thirdView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:thirdView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *sevenWidthConstraint = [NSLayoutConstraint
constraintWithItem:self.seven
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:thirdView
attribute:NSLayoutAttributeWidth
multiplier:0.3333
constant:thirdView.frame.size.width];
//두번째 뷰에 추가
[thirdView addConstraints:@[sevenTopConstraint, sevenLeftConstraint,sevenHeightConstraint,sevenWidthConstraint]];
//8번
/*버튼 생성*/
self.eight = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.eight.translatesAutoresizingMaskIntoConstraints = NO;
[self.eight.titleLabel setFont: [self.eight.titleLabel.font fontWithSize: 24]];
[self.eight setBackgroundColor:UIColor.orangeColor];
[self.eight setTitle:@"8" forState:UIControlStateNormal];
[self.eight addTarget:self action:@selector(numButtonAction:) forControlEvents: UIControlEventTouchUpInside];
[thirdView addSubview:self.eight];
//3. y 포지션
NSLayoutConstraint *eightTopConstraint = [NSLayoutConstraint
constraintWithItem:self.eight
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:thirdView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *eightLeftConstraint = [NSLayoutConstraint
constraintWithItem:self.eight
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.seven
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *eightHeightConstraint = [NSLayoutConstraint
constraintWithItem:self.eight
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:thirdView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:thirdView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *eightWidthConstraint = [NSLayoutConstraint
constraintWithItem:self.eight
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:thirdView
attribute:NSLayoutAttributeWidth
multiplier:0.3333
constant:thirdView.frame.size.width];
//두번째 뷰에 추가
[thirdView addConstraints:@[eightTopConstraint, eightLeftConstraint,eightHeightConstraint,eightWidthConstraint]];
//9번
/*버튼 생성*/
self.nine = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.nine.translatesAutoresizingMaskIntoConstraints = NO;
[self.nine.titleLabel setFont: [self.nine.titleLabel.font fontWithSize: 24]];
[self.nine setBackgroundColor:UIColor.yellowColor];
[self.nine setTitle:@"9" forState:UIControlStateNormal];
[self.nine addTarget:self action:@selector(numButtonAction:) forControlEvents: UIControlEventTouchUpInside];
[thirdView addSubview:self.nine];
//3. y 포지션
NSLayoutConstraint *nineTopConstraint = [NSLayoutConstraint
constraintWithItem:self.nine
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:thirdView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *nineLeftConstraint = [NSLayoutConstraint
constraintWithItem:self.nine
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.eight
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *nineHeightConstraint = [NSLayoutConstraint
constraintWithItem:self.nine
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:thirdView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:thirdView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *nineViewWidthConstraint = [NSLayoutConstraint
constraintWithItem:self.nine
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:thirdView
attribute:NSLayoutAttributeWidth
multiplier:0.3333
constant:thirdView.frame.size.width];
//두번째 뷰에 추가
[thirdView addConstraints:@[nineTopConstraint, nineLeftConstraint,nineHeightConstraint,nineViewWidthConstraint]];
#pragma four view - !
//전체 뷰
UIView *fourView = [[UIView alloc]init];
fourView.translatesAutoresizingMaskIntoConstraints = NO;
fourView.backgroundColor = UIColor.blueColor;
[totalView addSubview:fourView];
//3. y 포지션
NSLayoutConstraint *foureViewTopConstraint = [NSLayoutConstraint
constraintWithItem:fourView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:thirdView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *foureViewLeftConstraint = [NSLayoutConstraint
constraintWithItem:fourView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:totalView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *foureViewHeightConstraint = [NSLayoutConstraint
constraintWithItem:fourView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:totalView
attribute:NSLayoutAttributeHeight
multiplier:0.25
constant:totalView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *foureViewWidthConstraint = [NSLayoutConstraint
constraintWithItem:fourView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:totalView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:totalView.frame.size.width];
//토탈 뷰에 추가
[totalView addConstraints:@[foureViewTopConstraint, foureViewLeftConstraint,foureViewHeightConstraint,foureViewWidthConstraint]];
#pragma 버튼 clear 0번 back
/*버튼 생성*/
self.clearButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.clearButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.clearButton.titleLabel setFont: [self.clearButton.titleLabel.font fontWithSize: 24]];
[self.clearButton setBackgroundColor:UIColor.yellowColor];
[self.clearButton setTitle:@"clear" forState:UIControlStateNormal];
[self.clearButton addTarget:self action:@selector(numButtonAction:) forControlEvents: UIControlEventTouchUpInside];
[fourView addSubview:self.clearButton];
//3. y 포지션
NSLayoutConstraint *clearTopConstraint = [NSLayoutConstraint
constraintWithItem:self.clearButton
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:fourView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *clearLeftConstraint = [NSLayoutConstraint
constraintWithItem:self.clearButton
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:fourView
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *clearHeightConstraint = [NSLayoutConstraint
constraintWithItem:self.clearButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:fourView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:fourView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *clearWidthConstraint = [NSLayoutConstraint
constraintWithItem:self.clearButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:fourView
attribute:NSLayoutAttributeWidth
multiplier:0.3333
constant:fourView.frame.size.width];
//두번째 뷰에 추가
[fourView addConstraints:@[clearTopConstraint, clearLeftConstraint,clearWidthConstraint,clearHeightConstraint]];
//8번
/*버튼 생성*/
self.zero = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.zero.translatesAutoresizingMaskIntoConstraints = NO;
[self.zero setBackgroundColor:UIColor.greenColor];
[self.zero.titleLabel setFont: [self.zero.titleLabel.font fontWithSize: 24]];
[self.zero setTitle:@"8" forState:UIControlStateNormal];
[self.zero addTarget:self action:@selector(numButtonAction:) forControlEvents: UIControlEventTouchUpInside];
[fourView addSubview:self.zero];
//3. y 포지션
NSLayoutConstraint *zeroTopConstraint = [NSLayoutConstraint
constraintWithItem:self.zero
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:fourView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *zeroLeftConstraint = [NSLayoutConstraint
constraintWithItem:self.zero
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.clearButton
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *zeroHeightConstraint = [NSLayoutConstraint
constraintWithItem:self.zero
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:fourView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:fourView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *zeroWidthConstraint = [NSLayoutConstraint
constraintWithItem:self.zero
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:fourView
attribute:NSLayoutAttributeWidth
multiplier:0.3333
constant:fourView.frame.size.width];
//두번째 뷰에 추가
[fourView addConstraints:@[zeroTopConstraint, zeroLeftConstraint,zeroHeightConstraint,zeroWidthConstraint]];
// back
/*버튼 생성*/
self.deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.deleteButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.deleteButton setBackgroundColor:UIColor.orangeColor];
[self.deleteButton.titleLabel setFont: [self.deleteButton.titleLabel.font fontWithSize: 24]];
[self.deleteButton setTitle:@"back" forState:UIControlStateNormal];
[self.deleteButton addTarget:self action:@selector(numButtonAction:) forControlEvents: UIControlEventTouchUpInside];
[fourView addSubview:self.deleteButton];
//3. y 포지션
NSLayoutConstraint *deleteTopConstraint = [NSLayoutConstraint
constraintWithItem:self.deleteButton
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:fourView
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
//제약 조건 설정 왼쪽
NSLayoutConstraint *deleteLeftConstraint = [NSLayoutConstraint
constraintWithItem:self.deleteButton
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationGreaterThanOrEqual
toItem:self.zero
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
/* 높이 제약조건 */
NSLayoutConstraint *deleteHeightConstraint = [NSLayoutConstraint
constraintWithItem:self.deleteButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:fourView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:fourView.frame.size.height];
/* 가로 제약조건 */
NSLayoutConstraint *deleteWidthConstraint = [NSLayoutConstraint
constraintWithItem:self.deleteButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:fourView
attribute:NSLayoutAttributeWidth
multiplier:0.3333
constant:fourView.frame.size.width];
//두번째 뷰에 추가
[fourView addConstraints:@[deleteTopConstraint, deleteLeftConstraint,deleteHeightConstraint,deleteWidthConstraint]];
}
-(void)numButtonAction:(UIButton*)sender{
if([sender.titleLabel.text isEqualToString:@"clear"]){ //전체 지우기
NSLog(@"clear");
self.textfield.text = @"";
_secretKey = [[NSString alloc]init];
return;
}else if([sender.titleLabel.text isEqualToString:@"back"]){ //하나씩 지우기
NSLog(@"back");
if ([_secretKey length] > 0) {
_secretKey = [_secretKey substringToIndex:[_secretKey length] - 1];
self.textfield.text = _secretKey;
} else {
return;
}
return;
}else{ //숫자하나씩더하기
_secretKey = [_secretKey stringByAppendingString:sender.titleLabel.text];
self.textfield.text = _secretKey;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
file
참고
https://soooprmx.com/archives/7232
https://zeddios.tistory.com/380
http://www.tutorialspoint.com/ios/ios_auto_layouts.htm
'ios 뽀개기 > objective-c' 카테고리의 다른 글
AppDelegate 사용자 터치 감지하기 (0) | 2019.01.04 |
---|---|
최상위 viewController 클래스의 이름 가져오기 (0) | 2019.01.04 |
암호화 복호화 (0) | 2019.01.02 |
Obejctive c 클래스 변수 & 배열을 json 변환 (0) | 2019.01.02 |
UIView 커스터마이징 & 오디오 비디오 사진 뷰어 (0) | 2018.12.28 |
댓글