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

相关推荐

  • mkcert

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

    入门教程 2024年 1月 3日
  • Git 基本操作

    Git 的工作就是创建和保存你项目的快照及与之后的快照进行对比。 本章将对有关创建与提交你的项目快照的命令作介绍。 Git 常用的是以下 6 个命令:git clone、git p…

    2024年 4月 30日
  • tree

    文章目录tree补充说明语法选项参数实例 tree 树状图列出目录的内容 补充说明 tree命令 以树状图列出目录的内容。 语法 tree(选项)(参数) 选项 ——- 列…

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

    文章目录true概要主要用途返回值例子注意 true 返回状态为成功。 概要 true 主要用途 用于和其他命令进行逻辑运算。 返回值 返回状态总是成功;返回值为0。 例子 # 当…

    入门教程 2024年 3月 11日
  • TCP/IP 介绍

    TCP/IP 是用于因特网 (Internet) 的通信协议。 文章目录计算机通信协议(Computer Communication Protocol)什么是 TCP/IP?在 T…

    2023年 5月 15日
  • perl

    文章目录perl补充说明语法选项参数 perl perl语言解释器 补充说明 perl命令 是perl语言解释器,负责解释执行perl语言程序。 语法 perl(选项)(参数) 选…

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

    文章目录false概要主要用途返回值注意 false 返回状态为失败。 概要 false 主要用途 用于和其他命令进行逻辑运算。 返回值 返回状态总是失败;返回值为1。 注意 该命…

    入门教程 2023年 12月 14日
  • dpkg-query

    文章目录dpkg-query补充说明语法选项参数实例 dpkg-query Debian Linux中软件包的查询工具 补充说明 dpkg-query命令 是Debian Linu…

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

    文章目录dhcrelay补充说明语法选项例子 dhcrelay 使用dhcrelay命令可以提供中继DHCP和BOOTP请求 补充说明 dhcrelay命令 使用dhcrelay命…

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

    文章目录reboot补充说明语法选项实例 reboot 重新启动正在运行的Linux操作系统 补充说明 reboot命令 用来重新启动正在运行的Linux操作系统。 语法 rebo…

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