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

相关推荐

  • nproc

    文章目录nproc概要主要用途选项例子注意 nproc 打印可用的处理器单元数量。 概要 nproc [OPTION]… 主要用途 打印可用的处理器单元数量。 选项 –all…

    入门教程 2024年 1月 10日
  • df

    文章目录df补充说明语法选项参数大小格式实例 df 显示磁盘的相关信息 补充说明 df命令 用于显示磁盘分区上的可使用的磁盘空间。默认显示单位为KB。可以利用该命令来获取硬盘被占用…

    入门教程 2023年 12月 7日
  • Helm | Helm 包

    文章目录helm package简介可选项从父命令继承的命令请参阅 helm package 将chart目录打包到chart归档中 简介 该命令将chart打包成一个chart版…

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

    Java 是由 Sun Microsystems 公司于 1995 年 5 月推出的高级程序设计语言。 Java 可运行于多个平台,如 Windows, Mac OS 及其他多种 …

    2023年 3月 4日
  • Java 基本数据类型

    变量就是申请内存来存储值。也就是说,当创建变量的时候,需要在内存中申请空间。 内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据。 因此,通过定义不同类…

    2023年 3月 4日
  • uuto

    文章目录uuto补充说明语法例子 uuto 将文件传送到远端的UUCP主机 补充说明 uuto命令 为script文件,它实际上会执行uucp,用来将文件传送到远端UUCP主机,并…

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

    文章目录userdel补充说明语法选项参数实例 userdel 用于删除给定的用户以及与用户相关的文件 补充说明 userdel命令 用于删除给定的用户,以及与用户相关的文件。若不…

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

    文章目录blkid补充说明语法选项实例 blkid 查看块设备的文件系统类型、LABEL、UUID等信息 补充说明 在Linux下可以使用 blkid命令 对查询设备上所采用文件系…

    入门教程 2023年 12月 6日
  • chpasswd

    文章目录chpasswd补充说明语法选项实例 chpasswd 批量更新用户口令的工具 补充说明 chpasswd命令 是批量更新用户口令的工具,是把一个文件内容重新定向添加到/e…

    入门教程 2023年 12月 7日
  • pvcreate

    文章目录pvcreate补充说明语法选项参数实例 pvcreate 将物理硬盘分区初始化为物理卷 补充说明 pvcreate命令 用于将物理硬盘分区初始化为物理卷,以便LVM使用。…

    入门教程 2024年 3月 1日
Translate »