ios 의 빠 른 VFL 의 demo 이해
23423 단어 demo
http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/
https://github.com/ariok/TB_ProgrammaticallyAutoLayout 코드 데모
demo 코드 는 다음 과 같 습 니 다.
/* Initial views setup */
- (void)setupViews { self.redView = [UIView new]; self.redView.translatesAutoresizingMaskIntoConstraints = NO; self.redView.backgroundColor = [UIColor colorWithRed:0.95 green:0.47 blue:0.48 alpha:1.0]; self.yellowView = [UIView new]; self.yellowView.translatesAutoresizingMaskIntoConstraints = NO; self.yellowView.backgroundColor = [UIColor colorWithRed:1.00 green:0.83 blue:0.58 alpha:1.0]; [self.view addSubview:self.redView]; [self.view addSubview:self.yellowView]; } /* Hey Devs... the code in the next functions has to be intended for tutorial purposes only. I have created work-alone examples that contain a lot of code duplication... not a good practice but way easier to explain :P */
/* EXAMPLE 1 */
- (void)example_1 { // 1. Create a dictionary of views
NSDictionary *viewsDictionary = @{@"redView":self.redView}; // 2. Define the redView Size
NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView(100)]" options:0 metrics:nil views:viewsDictionary]; NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(100)]" options:0 metrics:nil views:viewsDictionary]; [self.redView addConstraints:constraint_H]; [self.redView addConstraints:constraint_V]; // 3. Define the redView Position
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[redView]" options:0 metrics:nil views:viewsDictionary]; NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[redView]" options:0 metrics:nil views:viewsDictionary]; // 3.B ...and try to change the visual format string //NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView]-30-|" options:0 metrics:nil views:viewsDictionary]; //NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[redView]" options:0 metrics:nil views:viewsDictionary];
[self.view addConstraints:constraint_POS_H]; [self.view addConstraints:constraint_POS_V]; } /* EXAMPLE 2 */
- (void)example_2 { // 1. Create a dictionary of views
NSDictionary *viewsDictionary = @{@"redView":self.redView, @"yellowView":self.yellowView}; // 2. Define the views Sizes
NSArray *red_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView(100)]" options:0 metrics:nil views:viewsDictionary]; NSArray *red_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(100)]" options:0 metrics:nil views:viewsDictionary]; [self.redView addConstraints:red_constraint_H]; [self.redView addConstraints:red_constraint_V]; NSArray *yellow_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[yellowView(200)]" options:0 metrics:nil views:viewsDictionary]; NSArray *yellow_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[yellowView(100)]" options:0 metrics:nil views:viewsDictionary]; [self.yellowView addConstraints:yellow_constraint_H]; [self.yellowView addConstraints:yellow_constraint_V]; // 3. Define the views Positions
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-30-[redView]-40-[yellowView]" options:0 metrics:nil views:viewsDictionary]; NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[redView]-10-[yellowView]" options:0 metrics:nil views:viewsDictionary]; [self.view addConstraints:constraint_POS_V]; [self.view addConstraints:constraint_POS_H]; } /* EXAMPLE 3 */
- (void)example_3 { // 1. Create a dictionary of views
NSDictionary *viewsDictionary = @{@"redView":self.redView, @"yellowView":self.yellowView}; // 2. Define the views Sizes
NSArray *red_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView(100)]" options:0 metrics:nil views:viewsDictionary]; NSArray *red_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(100)]" options:0 metrics:nil views:viewsDictionary]; [self.redView addConstraints:red_constraint_H]; [self.redView addConstraints:red_constraint_V]; NSArray *yellow_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[yellowView(150)]" options:0 metrics:nil views:viewsDictionary]; NSArray *yellow_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[yellowView(100)]" options:0 metrics:nil views:viewsDictionary]; [self.yellowView addConstraints:yellow_constraint_H]; [self.yellowView addConstraints:yellow_constraint_V]; // 3. Define the views Positions using options
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-120-[redView]" options:0 metrics:nil views:viewsDictionary]; NSArray *constraint_POS = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[redView]-10-[yellowView]" options:NSLayoutFormatAlignAllTop metrics:nil views:viewsDictionary]; [self.view addConstraints:constraint_POS_V]; [self.view addConstraints:constraint_POS]; } /* EXAMPLE 4 */
- (void)example_4 { // 1. Create a dictionary of views and metrics
NSDictionary *viewsDictionary = @{@"redView":self.redView, @"yellowView":self.yellowView}; NSDictionary *metrics = @{@"redWidth": @100, @"redHeight": @100, @"yellowWidth": @100, @"yellowHeight": @150, @"topMargin": @120, @"leftMargin": @20, @"viewSpacing":@10 }; // 2. Define the views Sizes
NSArray *red_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView(redHeight)]" options:0 metrics:metrics views:viewsDictionary]; NSArray *red_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(redWidth)]" options:0 metrics:metrics views:viewsDictionary]; [self.redView addConstraints:red_constraint_H]; [self.redView addConstraints:red_constraint_V]; NSArray *yellow_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[yellowView(yellowHeight)]" options:0 metrics:metrics views:viewsDictionary]; NSArray *yellow_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[yellowView(yellowWidth)]" options:0 metrics:metrics views:viewsDictionary]; [self.yellowView addConstraints:yellow_constraint_H]; [self.yellowView addConstraints:yellow_constraint_V]; // 3. Define the views Positions
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-topMargin-[redView]" options:0 metrics:metrics views:viewsDictionary]; NSArray *constraint_POS = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-leftMargin-[redView]-viewSpacing-[yellowView]" options:NSLayoutFormatAlignAllTop metrics:metrics views:viewsDictionary]; [self.view addConstraints:constraint_POS_V]; [self.view addConstraints:constraint_POS]; } /* EXAMPLE 5 */
- (void)example_5 { // 1. Create a dictionary of views and metrics
NSDictionary *viewsDictionary = @{@"redView":self.redView}; NSDictionary *metrics = @{@"vSpacing":@30, @"hSpacing":@10}; // 2. Define the view Position and automatically the Size
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vSpacing-[redView]-vSpacing-|" options:0 metrics:metrics views:viewsDictionary]; NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[redView]-hSpacing-|" options:0 metrics:metrics views:viewsDictionary]; [self.view addConstraints:constraint_POS_V]; [self.view addConstraints:constraint_POS_H]; } /* EXAMPLE 6 */
- (void)example_6 { // 1. Create a dictionary of views
NSDictionary *viewsDictionary = @{@"redView": self.redView, @"yellowView": self.yellowView}; NSDictionary *metrics = @{@"vSpacing":@30, @"hSpacing":@10}; // 2. Define the view Position and automatically the Size (for the redView)
NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-vSpacing-[redView]-vSpacing-|" options:0 metrics:metrics views:viewsDictionary]; NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-hSpacing-[redView]-hSpacing-|" options:0 metrics:metrics views:viewsDictionary]; [self.view addConstraints:constraint_POS_V]; [self.view addConstraints:constraint_POS_H]; // 3. Define sizes thanks to relations with another view (yellowView in relation with redView)
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.yellowView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.yellowView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeHeight multiplier:0.5 constant:0.0]]; // 4. Define position thanks to relations with another view (yellowView in relation with redView)
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.yellowView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]]; [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.yellowView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.redView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]]; }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
msdn에서 인증서 설정 데이터베이스 이미지 데모[페이지 맨 위로 돌아가기] 인바운드 연결 구성 인바운드 연결을 위한 Host_ 구성A HOST에서_A에서 HOST_B로그인 이름을 만듭니다.USE master; CREATE LOGIN HOST_B_login WIT...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.