iOS UI-自动布局(AutoLayout)

  1 //
  2 //  ViewController.m
  3 //  IOS_0115_AutoLayout
  4 //
  5 //  Created by ma c on 16/1/15.
  6 //  Copyright (c) 2016年 博文科技. All rights reserved.
  7 //
  8
  9 #import "ViewController.h"
 10
 11 @interface ViewController ()
 12
 13 @property (nonatomic, strong) NSLayoutConstraint *changeBlueTop;
 14 @property (nonatomic, strong) UIView *blueView;
 15
 16
 17
 18 @end
 19
 20 @implementation ViewController
 21
 22 - (void)viewDidLoad {
 23     [super viewDidLoad];
 24
 25     //创建蓝色View
 26     UIView *blueView = [[UIView alloc] init];
 27     blueView.frame = CGRectMake(0, 0, 100, 100);
 28     blueView.backgroundColor = [UIColor blueColor];
 29     [self.view addSubview:blueView];
 30     self.blueView = blueView;
 31     //创建红色View
 32     UIView *redView = [[UIView alloc] init];
 33     redView.frame = CGRectMake(100, 100, 100, 100);
 34     redView.backgroundColor = [UIColor redColor];
 35     [blueView addSubview:redView];
 36
 37     //禁用autoresizing
 38     blueView.translatesAutoresizingMaskIntoConstraints = NO;
 39     redView.translatesAutoresizingMaskIntoConstraints = NO;
 40
 41     //创建并添加约束
 42
 43     //1.创建蓝色View的约束
 44
 45     /*
 46      (id)对象 的 (NSLayoutAttribute)属性
 47      (NSLayoutRelation)关系(> = <)
 48      (id)对象的(NSLayoutAttribute)属性
 49      乘以multiplier的参数 加上constant的参数
 50      */
 51
 52     //高度的约束
 53     NSLayoutConstraint *blueHeight = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50];
 54     //把约束加到控件上
 55     [blueView addConstraint:blueHeight];
 56
 57     //距离左边30
 58     NSLayoutConstraint *blueLeft = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:30];
 59     [blueView.superview addConstraint:blueLeft];
 60
 61     //距离上面30(topLayoutGuide状态栏)
 62     NSLayoutConstraint *blueTop = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:30];
 63     [blueView.superview addConstraint:blueTop];
 64     self.changeBlueTop = blueTop;
 65
 66
 67     //距离右边30
 68     NSLayoutConstraint *blueRight = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:-30];
 69     [blueView.superview addConstraint:blueRight];
 70
 71     //1.创建红色View的约束
 72     //红色View的高度等于蓝色View的高度
 73     NSLayoutConstraint *redHeight = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
 74     [redView.superview addConstraint:redHeight];
 75
 76     //红色view的Top距离蓝色View的Bottom30
 77     NSLayoutConstraint *redTop = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:30];
 78     [redView.superview addConstraint:redTop];
 79
 80     //红色View与蓝色View右对齐
 81     NSLayoutConstraint *redRight = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
 82     [redView.superview addConstraint:redRight];
 83
 84     //红色View的宽度等于蓝色View的宽度的一半
 85     NSLayoutConstraint *redWidth = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0];
 86     [redView.superview addConstraint:redWidth];
 87
 88     //修改约束实现动画效果
 89
 90     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
 91
 92     [btn setFrame:CGRectMake(0, 300, 50, 50)];
 93     [btn setTitle:@"移动" forState:UIControlStateNormal];
 94     [btn setBackgroundColor:[UIColor orangeColor]];
 95     [self.view addSubview:btn];
 96     [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
 97
 98 }
 99
100 - (void)btnClick
101 {
102     //修改约束-没有根据新的约束计算蓝色View的frame
103     self.changeBlueTop.constant += 50;
104     [UIView animateWithDuration:1 animations:^{
105         //根据新的约束修改新的frame
106         [self.blueView layoutIfNeeded];
107     }];
108 }
109
110
111 - (void)didReceiveMemoryWarning {
112     [super didReceiveMemoryWarning];
113     // Dispose of any resources that can be recreated.
114 }
115
116 @end
时间: 2024-08-26 09:44:11

iOS UI-自动布局(AutoLayout)的相关文章

ios使用自动布局autolayout

在ios开发中经常用到自动布局,比如屏幕旋转来,界面就要从新摆放,不然就会显示不全. 自动布局一定要在storyboard界面里面设置,不能用纯代码的形式来写 要使用自动布局,首先要确保interface builder document里面的Use Auto Layout 和Use Size Classes 选项已经勾选??上了 图1:打开auto layout 这样在storyboard的右下角就会出现几个图形按钮:   分别是:stack.align.pin.和resolve auto l

iOS的自动布局

iOS的自动布局 一.StoryBoard和Xib的简介 在这之前先简略介绍一下Xib/nib和StoryBoard. Xib/nib其实可以认为是一样的,nib是iOS3.0前的产物,它的本质是一个装着可执行二进制文件的文件夹.Xib的本质则是一个xml类型的描述文件,可以实现可视化的编程.两者在在UIViewController的生命周期方法loadView方法前都会转换成可执行的nib文件. StoryBoard是多个Xib的集合的描述,也是xml格式的. Storyboard和Xib的区

自动布局AutoLayout

Auto Layout 是iOS6发布后引入的一个全新的布局特性,其目的是弥补以往autoresizing在布局方面的不足之处,以及未来面对更多尺寸适配时界面布局可以更好的适应. 要完全掌握 Auto Layout 是一件非常消耗精力的事情,需要大量的实践,并且在根本上面,理解其如何使用,如果要全面的介绍 Auto Layout 和使用场景估计几篇博文都介绍不完, 本文希望能将使用 Auto Layout 的重点和技巧以及注意事项,进行一个介绍.成为学习 Auto Layout 的一个导航文章.

iOS 之自动布局

     项目要做iPhone版和iPad的适配,就找了一些资料 关于iOS的自动布局,学习的一些收获以及心得给大家分享一下.       xib的布局就不说了,就是线的连接,主要分享一下纯代码的一些自动布局的学习心得.       Autolayout的强大是毋庸质疑的,当你熟悉了它之后,你肯定会觉得它很方便的实现布局,布局将会比使用frame的绝对坐标时还方便.      UIView *superview = self; UIView *view1 = [[UIView alloc] in

自动布局(AutoLayout)使用

/*1. 创建控件,并添加到当前的View中*/ self.rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; self.rightButton.translatesAutoresizingMaskIntoConstraints = NO; [self.rightButton setTitle:@"RightButton" forState:UIControlStateNormal]; [self.view 

如何解决IOS 动画中 Autolayout 与View Transforms的冲突

IOS 的动画放大与缩小,并非按照找它的中心点放大和缩小,而是左上角 .我分析了下原来是Autolayout 与View Transforms的冲突造成的. - (void) addSubviewWithZoomInAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option { // first reduce the view to 1/100th of its original dimen

IOS Ui控件 修改位置和尺寸,代码添加控件

所有的UI控件最终都继承自UIView,UI控件的公共属性都定义在UIView中, UIView的常见属性 UIView *superview; 获得自己的父控件对象 NSArray *subviews; 获得自己的所有子控件对象 NSInteger tag; 控件的ID(标识),父控件可以通过tag来找到对应的子控件 CGAffineTransform transform; 控件的形变属性(可以设置旋转角度.比例缩放.平移等属性) CGRect frame; 控件所在矩形框在父控件中的位置和尺

iOS之自动布局

iOS的自动布局技术一直都是前端开发所必不可少的,它能使我们开发出来的项目更加规范美观,同时也更加灵活 ,接下来笔者就介绍一下自动布局常用的几种方式,供大家参考~~ 方法一:storyboard 从一开始做iOS开发,只考虑适配4s,直接把坐标,长宽都写成固定值. 之后考虑适配5s,在界面上设定好一 bbs.chinaacc.com/forum-2-26/topic-5619705.html bbs.chinaacc.com/forum-2-26/topic-5619701.html bbs.c

【腾讯TMQ】解放程序猿(媛)的双手—iOS UI自动化测试

解放程序猿(媛)的双手-iOS UI自动化测试 前言 随着移动互联网时代的蓬勃发展,移动终端的自动化测试也在业界日益活跃,总体来看在Android平台上的自动化工具和实践比较多,但是说到iOS平台无论从自动化工具的数量还是质量上就陡降了.究其原因,无外乎是iOS系统的封闭性,加上相对Android用户的数量少,导致对这个平台系统的自动化进展缓慢,据笔者了解到的情况,很多iOS平台的测试人员还处于纯手工测试模式,自动化程度和Android平台无法相论,更别提和PC端相比了. 然而越是困难的事,越是

ios之用Autolayout均匀平铺排列多个button按钮

前言: iPhone的屏幕越来越大,然而适配起来比较复杂,不得不用Autolayout来写代码,今天就简单的实现了一个均匀平铺view的功能.学会了这个功能以后,以后所有的适配都是小cass了吧!哈哈(我这么认为). 请看效果: 实现方式就不在博客里写了,直接看Demo吧,很简单的. Demo下载地址:ios之用Autolayout均匀平铺排列多个button按钮