OC - 自动布局

OC - 自动布局 首页 / iOS入门教程 / OC - 自动布局

自动布局是在 iOS 6.0中引入的,使用自动布局时,无涯教程的部署目标应为6.0或更高版本,自动布局可帮助无涯教程创建可用于多种方向和多种设备的界面。

无涯教程将在代码中添加一个文本字段和两个按钮及其约束。将创建每个UI元素的约束并将其添加到超级视图。为了获得所需的输出,无涯教程将不得不为添加的每个UI元素禁用自动调整大小。

涉及步骤

步骤1 - 创建一个基于视图的简单应用程序。

步骤2 - 无涯教程将仅编辑ViewController.m,如下所示-

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic strong) UIButton *leftButton;
@property (nonatomic strong) UIButton *rightButton;
@property (nonatomic strong) UITextField *textfield;

@end
@implementation ViewController

- (void)viewDidLoad {
   [super viewDidLoad];
   UIView *superview = self.view;

   /*1. 创建 leftButton 并添加到我们的视图中*/
   self.leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   self.leftButton.translatesAutoresizingMaskIntoConstraints = NO;
   [self.leftButton setTitle:@"LeftButton" forState:UIControlStateNormal];
   [self.view addSubview:self.leftButton];

   /* 2. 约束定位 LeftButton 的 X*/
   NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint 
   constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterX 
   relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
   NSLayoutAttributeCenterX multiplier:1.0 constant:-60.0f];

   /* 3. 约束定位 LeftButton 的 Y*/
   NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint 
   constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterY 
   relatedBy:NSLayoutRelationEqual toItem:superview attribute:
   NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];

   /* 4. 将约束添加到按钮的superview*/
   [superview addConstraints:@[ leftButtonXConstraint
   leftButtonYConstraint]];

   /*5. 创建 rightButton 并添加到我们的视图中*/
   self.rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   self.rightButton.translatesAutoresizingMaskIntoConstraints = NO;
   [self.rightButton setTitle:@"RightButton" forState:UIControlStateNormal];
   [self.view addSubview:self.rightButton];

   /*6. 约束 RightButton 的 X 位置*/
   NSLayoutConstraint *rightButtonXConstraint = [NSLayoutConstraint 
   constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterX 
   relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
   NSLayoutAttributeCenterX multiplier:1.0 constant:60.0f];

   /*7. 约束 RightButton 的 Y 位置*/
   rightButtonXConstraint.priority = UILayoutPriorityDefaultHigh;
   NSLayoutConstraint *centerYMyConstraint = [NSLayoutConstraint 
   constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterY 
   relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
   NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];
   [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];

   //9. 文本字段约束
   NSLayoutConstraint *textFieldTopConstraint = [NSLayoutConstraint 
   constraintWithItem:self.textfield attribute:NSLayoutAttributeTop 
   relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview 
   attribute:NSLayoutAttributeTop multiplier:1.0 constant:60.0f];
   NSLayoutConstraint *textFieldBottomConstraint = [NSLayoutConstraint 
   constraintWithItem:self.textfield attribute:NSLayoutAttributeTop 
   relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self.rightButton 
   attribute:NSLayoutAttributeTop multiplier:0.8 constant:-60.0f];
   NSLayoutConstraint *textFieldLeftConstraint = [NSLayoutConstraint 
   constraintWithItem:self.textfield attribute:NSLayoutAttributeLeft 
   relatedBy:NSLayoutRelationEqual toItem:superview attribute:
   NSLayoutAttributeLeft multiplier:1.0 constant:30.0f];
   NSLayoutConstraint *textFieldRightConstraint = [NSLayoutConstraint 
   constraintWithItem:self.textfield attribute:NSLayoutAttributeRight 
   relatedBy:NSLayoutRelationEqual toItem:superview attribute:
   NSLayoutAttributeRight multiplier:1.0 constant:-30.0f];
   [superview addConstraints:@[textFieldBottomConstraint 
   textFieldLeftConstraint textFieldRightConstraint 
   textFieldTopConstraint]];
}

- (void)didReceiveMemoryWarning {
   [super didReceiveMemoryWarning];
   //Dispose of any resources that can be recreated.
}
@end

注意事项

在标签为1、5和8的步骤中,无涯教程分别以编程方式添加了两个按钮和一个文本字段。

在其余步骤中,无涯教程创建了约束,并将这些约束添加到了相应的超级视图中,这些超级视图实际上是自视图。左按钮之一的约束如下所示-

链接:https://www.learnfk.comhttps://www.learnfk.com/ios/ios-auto-layouts.html

来源:LearnFk无涯教程网

NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint 
constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterX 
relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
NSLayoutAttributeCenterX multiplier:1.0 constant:-60.0f];

无涯教程有constraintWithItem和toItem,它们决定无涯教程在哪个UI元素之间创建约束。该属性决定将两个元素链接在一起的依据。 " relatedBy"决定属性在元素之间的影响。乘数是乘数,常数将被添加到乘数中。

在上面的示例中,leftButton的X相对于超级视图的中心始终大于或等于-60像素。类似地,定义了其他约束。

运行应用程序时,无涯教程将在iPhone模拟器上获得以下输出-

iOS Tutorial

当无涯教程将模拟器的方向更改为横向时,无涯教程将获得以下输出-

iOS Tutorial

当无涯教程在iPhone 5模拟器上运行相同的应用程序时,无涯教程将获得以下输出-

无涯教程网

iOS Tutorial

当无涯教程将模拟器的方向更改为横向时,无涯教程将获得以下输出-

iOS Tutorial

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

程序员的数学基础课 -〔黄申〕

软件工程之美 -〔宝玉〕

全栈工程师修炼指南 -〔熊燚(四火)〕

说透中台 -〔王健〕

设计模式之美 -〔王争〕

Django快速开发实战 -〔吕召刚〕

实用密码学 -〔范学雷〕

郭东白的架构课 -〔郭东白〕

自动化测试高手课 -〔柳胜〕

好记忆不如烂笔头。留下您的足迹吧 :)