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

相关推荐

  • mapfile

    文章目录mapfile概要主要用途选项参数返回值例子注意 mapfile 从标准输入读取行并赋值到数组。 概要 mapfile [-d delim] [-n count] [-O …

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

    文章目录mkbootdisk补充说明语法选项参数实例 mkbootdisk 可建立目前系统的启动盘 补充说明 mkbootdisk命令 用来为当前运行的系统创建能够单独使用的系统引…

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

    文章目录tailf补充说明语法选项参数实例 tailf 在屏幕上显示指定文件的末尾若干行内容,通常用于日志文件的跟踪输出 补充说明 tailf命令几乎等同于tail -f,严格说来…

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

    文章目录dnf补充说明安装 DNF 包管理器总结 dnf 新一代的RPM软件包管理器 补充说明 DNF 是新一代的rpm软件包管理器。他首先出现在 Fedora 18 这个发行版中…

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

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

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

    文章目录arpwatch补充说明语法选项 arpwatch 监听网络上ARP的记录 补充说明 arpwatch命令 用来监听网络上arp的记录。 语法 arpwatch(选项) 选…

    入门教程 2023年 12月 6日
  • Helm | Helm 仓库索引

    文章目录helm repo index简介可选项从父命令继承的命令请参阅 helm repo index 基于包含打包chart的目录,生成索引文件 简介 读取当前目录,并根据找到…

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

    文章目录findfs补充说明语法参数实例 findfs 标签或UUID查找文件系统 补充说明 findfs命令 依据卷标(Label)和UUID查找文件系统所对应的设备文件。fin…

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

    文章目录lvdisplay补充说明语法参数实例 lvdisplay 显示逻辑卷属性 补充说明 lvdisplay命令 用于显示LVM逻辑卷空间大小、读写状态和快照信息等属性。如果省…

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

    文章目录netstat补充说明语法选项实例扩展知识网络连接状态详解 netstat 查看Linux中网络系统状态信息 补充说明 netstat命令 用来打印Linux中网络系统的状…

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