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

相关推荐

  • tac

    文章目录tac概要主要用途参数选项返回值例子注意 tac 连接多个文件并以行为单位反向打印到标准输出。 概要 tac [OPTION]… [FILE]… 主要用途 按行为单…

    入门教程 2024年 3月 11日
  • IOS 故事板(Storyboards)

    简介 Storyboards在 iOS 5 中才有介绍,当我们用Storyboards时,部署目标应该是iOS5.0或更高版本。 Storyboards 帮助我们了解视觉流动的画面…

    2023年 4月 1日
  • iptables-restore

    文章目录iptables-restore补充说明语法选项实例 iptables-restore 还原iptables表的配置 补充说明 iptables-restore命令 用来还…

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

    文章目录mailstat补充说明语法选项参数 mailstat 显示到达的邮件状态 补充说明 mailstat命令 用来显示到达的邮件状态。 语法 mailstat(选项)(参数)…

    入门教程 2024年 1月 3日
  • 32. panic 和 recover

    欢迎来到 Golang 系列教程的第 32 篇。 文章目录什么是 panic?什么时候应该使用 panic?panic 示例发生 panic 时的 deferrecoverpani…

    2023年 12月 5日
  • dpkg-statoverride

    文章目录dpkg-statoverride补充说明语法选项实例 dpkg-statoverride Debian Linux中覆盖文件的所有权和模式 补充说明 dpkg-stato…

    入门教程 2023年 12月 7日
  • HTML 表格

    表格由 <table> 标签来定义。每个表格均有若干行(由 <tr> 标签定义),每行被分割为若干单元格(由 <td> 标签定义)。字母 td …

    2023年 4月 13日
  • ppp-off

    文章目录ppp-off补充说明语法 ppp-off 关闭ppp连线 补充说明 这是Slackware发行版内附的程序,让用户切断PPP的网络连线。 语法 ppp-off

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

    文章目录eject补充说明语法选项参数 eject 用来退出抽取式设备 补充说明 eject命令 用来退出抽取式设备。若设备已挂入,则eject命令会先将该设备卸除再退出。 eje…

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

    文章目录telnet补充说明语法选项参数实例 telnet 登录远程主机和管理(测试ip端口是否连通) 补充说明 telnet命令 用于登录远程主机,对远程主机进行管理。telne…

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