iOS自动布局

IOS自动布局


简介

自动布局在iOS 6.0中引入,仅可以支持IOS6.0 及 更高版本。它可以帮助我们创建用于多个种设备的界面。

实例步骤

1.创建一个简单的 View based application

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. Create leftButton and add to our view*/
    self.leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.leftButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.leftButton setTitle:@"LeftButton" forState:UIControlStateNormal];
    [self.view addSubview:self.leftButton];    
    /* 2. Constraint to position LeftButton's X*/
    NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint 
    constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterX 
    relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
    NSLayoutAttributeCenterX multiplier:1.0 constant:-60.0f];
    /* 3. Constraint to position LeftButton's Y*/
    NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint 
    constraintWithItem:self.leftButton attribute:NSLayoutAttributeCenterY 
    relatedBy:NSLayoutRelationEqual toItem:superview attribute:
    NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f];   
    /* 4. Add the constraints to button's superview*/
    [superview addConstraints:@[ leftButtonXConstraint,
    leftButtonYConstraint]];    
    /*5. Create rightButton and add to our view*/
    self.rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.rightButton.translatesAutoresizingMaskIntoConstraints = NO;
    [self.rightButton setTitle:@"RightButton" forState:UIControlStateNormal];
    [self.view addSubview:self.rightButton];    
    /*6. Constraint to position RightButton's X*/
    NSLayoutConstraint *rightButtonXConstraint = [NSLayoutConstraint 
    constraintWithItem:self.rightButton attribute:NSLayoutAttributeCenterX 
    relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:superview attribute:
    NSLayoutAttributeCenterX multiplier:1.0 constant:60.0f];
    /*7. Constraint to position RightButton's 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. Add Text field
    self.textfield = [[UITextField alloc]initWithFrame:
    CGRectMake(0, 100, 100, 30)];
    self.textfield.borderStyle = UITextBorderStyleRoundedRect;
    self.textfield.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:self.textfield];
    //9. Text field Constraints
    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

输出

运行应用程序,在 iPhone 模拟器上会有下面的输出结果

file

当我们更改模拟器为横向的方向时,输出结果如下

file

我们在 iPhone 5 模拟器上运行同一应用程序时,输出结果如下

file

当我们更改模拟器为横向的方向时,输出结果如下:

file

若文章对你有帮助,可以点赞或打赏支持我们。发布者:lyh会员,转载请注明出处:http://61.174.243.28:13541/AY-knowledg-hub/ios%e8%87%aa%e5%8a%a8%e5%b8%83%e5%b1%80/

(0)
lyhlyh会员认证作者
上一篇 2023年 4月 1日 下午9:10
下一篇 2023年 4月 1日 下午9:19

相关推荐

  • iconv

    文章目录iconv补充说明语法选项实例 iconv 转换文件的编码方式 补充说明 iconv命令 是用来转换文件的编码方式的,比如它可以将UTF8编码的转换成GB18030的编码,…

    入门教程 2023年 12月 19日
  • pidof

    文章目录pidof补充说明语法选项参数实例 pidof 查找指定名称的进程的进程号ID号 补充说明 pidof命令 用于查找指定名称的进程的进程号id号。 语法 pidof(选项)…

    入门教程 2024年 3月 1日
  • Mutex

    文章目录MutexMutex含有竞态条件的程序使用 Mutex使用信道处理竞态条件Mutex vs 信道 Mutex 上一节:第二十四篇 Select下一节:第二十六篇 结构体取代…

    2023年 12月 5日
  • 33. 函数是一等公民(头等函数)

    欢迎来到 Golang 系列教程的第 33 篇。 文章目录什么是头等(第一类)函数?匿名函数用户自定义的函数类型高阶函数把函数作为参数,传递给其它函数在其它函数中返回函数闭包头等函…

    2023年 12月 5日
  • unzip

    文章目录unzip补充说明语法选项参数实例 unzip 用于解压缩由zip命令压缩的压缩包 补充说明 unzip命令 用于解压缩由zip命令压缩的“.zip”压缩包。 语法 unz…

    入门教程 2024年 3月 11日
  • mkcert

    文章目录mkcert示例安装macOSLinuxWindows支持的 root stores mkcert 用来生成自签证书的工具 示例 mkcert 是 GO 编写的,一个简单的…

    入门教程 2024年 1月 3日
  • htop

    文章目录htop补充说明htop 安装语法参数参数示例选项Htop设定1. Meters2. Display options3. Colors4. Colums htop [非内部…

    入门教程 2023年 12月 15日
  • find

    文章目录find补充说明语法选项参数实例根据文件或者正则表达式进行匹配否定参数根据文件类型进行搜索基于目录深度搜索根据文件时间戳进行搜索根据文件大小进行匹配删除匹配文件根据文件权限…

    入门教程 2023年 12月 14日
  • YAML 入门教程

    YAML 是 YAML Ain\’t a Markup Language(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:Yet A…

    2023年 5月 18日
  • builtin

    文章目录builtin概要主要用途参数返回值例子注意 builtin 执行bash内建命令。 概要 builtin [shell-builtin [arg …]] 主要用途 用…

    入门教程 2023年 12月 6日
Translate »