Autolayout代码实现举例-01使用线性公式

1.例子1

  需求: 令一个宽高都为200的view永远显示在屏幕的中央。

 1    // 1.创建蓝色view
 2     UIView *blueView =[[UIView alloc] init];
 3     blueView.backgroundColor = [UIColor blueColor];
 4         // 使用Autolayout必须关闭下面这个属性, 意思是不要把AutoresizingMask应用到Contrainsts中
 5     blueView.translatesAutoresizingMaskIntoConstraints = NO;
 6         // 使用Autolayout之前必须把view加到父视图上
 7     [self.view addSubview:blueView];
 8
 9
10     // 2.对红色view添加约束
11     // 2.1.约束宽度
12     CGFloat width = 200;
13         // 创建约束对象, 实际是就是在表述线性公式  y = ax + b;
14     NSLayoutConstraint *widthConst = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:width];
15         // 与其他view无关的约束只需加在自己的约束集合中
16     [blueView addConstraint:widthConst];
17
18     // 2.2.约束高度
19     CGFloat height = 200;
20     NSLayoutConstraint *heightConst = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:height];
21     [blueView addConstraint:heightConst];
22
23     // 2.3.约束中心位置
24     NSLayoutConstraint *centerXConst = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
25         // 与其他view有关的约束加在离自己最近的父视图上
26     [self.view addConstraint:centerXConst];
27
28     NSLayoutConstraint *centerYConst = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
29     [self.view addConstraint:centerYConst];

结果:  竖屏 :     横屏: 

2.例子2

  需求: 创建两个View, 一个蓝色, 一个红色,  蓝色距离屏幕最上面、左边、右边的距离为20, 使红色view的顶部与蓝色view的底部的间距为20,且两者的右边缘对齐。

 1   // 1.蓝色view
 2     UIView *blueView =[[UIView alloc] init];
 3     blueView.backgroundColor = [UIColor blueColor];
 4     blueView.translatesAutoresizingMaskIntoConstraints = NO;
 5     [self.view addSubview:blueView];
 6
 7     // 2.红色view
 8     UIView *redView =[[UIView alloc] init];
 9     redView.backgroundColor = [UIColor redColor];
10     redView.translatesAutoresizingMaskIntoConstraints = NO;
11     [self.view addSubview:redView];
12
13     // 3.蓝色view的约束
14     // 3.1.高度
15     CGFloat height = 40;
16     NSLayoutConstraint *blueHeight = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:height];
17     [blueView addConstraint:blueHeight];
18
19     // 3.2.左边间距
20     CGFloat margin = 20;
21     NSLayoutConstraint *leftMargin = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:margin];
22     [self.view addConstraint:leftMargin];
23
24     // 3.3.右边间距
25         // 注意constant的正负
26     NSLayoutConstraint *rightMargin = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-margin];
27     [self.view addConstraint:rightMargin];
28
29     // 3.4.顶部间距
30     NSLayoutConstraint *topMargin = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:margin];
31     [self.view addConstraint:topMargin];
32
33     // 4.红色view的约束
34     // 4.1.高度
35         // 注意这里redView的高度关联到blueView, 所以此约束必须加到他们共同的(且为最近的)父视图上, 否则引发崩溃
36     NSLayoutConstraint *redHeight = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0];
37     [self.view addConstraint:redHeight];
38
39     // 4.2.左边缘
40     NSLayoutConstraint *leftEadge = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
41     [self.view addConstraint:leftEadge];
42
43     // 4.3.右边间距 == blueView
44     NSLayoutConstraint *rightMarginRed = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0];
45     [self.view addConstraint:rightMarginRed];
46
47     // 4.4.顶部间距 == blueView底部 + 20
48     NSLayoutConstraint *topMarginRed = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:margin];
49     [self.view addConstraint:topMarginRed];

结果:  竖屏 :   横屏:

时间: 2024-08-08 09:42:00

Autolayout代码实现举例-01使用线性公式的相关文章

Autolayout代码实现举例-02-使用VFL

0.简介: VFL 全称 Visual Format Language, 它能抽象表述一个或多个view的布局. 相比用线性公式实现Autolayout, VFL显得简便一些. 1.例子1 需求: 创建两个View, 一个蓝色, 一个红色,  蓝色距离屏幕最上面.左边.右边的距离为20, 使红色view的顶部与蓝色view的底部的间距为20,且两者的右边缘对齐. 结果:  竖屏 :     横屏:  2.例子2 需求: 创建两个View, 一个蓝色, 一个红色, 需求是使蓝色和红色view永远粘

如何在Word中优雅的插入Latex线性公式

写论文的小伙伴应该都有过这样的感受!普通二次公式的手动插入如果说是尚可忍受的话,那么做人工智能学习和物理研究的小伙伴在插入二项式定理和傅立叶公式的时候,如果是手动输入....我想不必多说了,下面我就来介绍下,如果配合Mathpix在word中优雅的输入基于Latex的线性公式. LaTeX 作为一款「史诗级」文章排版编译器,一直都有着优秀.高效的排版体验和简洁.一致的排版效果.但是 LaTeX 相对复杂的语法使用,让我们很多时候都需要花费大量时间在查阅 LaTeX 的参考文档上,才能得到我们想要

【Python 代码】CS231n中Softmax线性分类器、非线性分类器对比举例(含python绘图显示结果)

1 #CS231n中线性.非线性分类器举例(Softmax) 2 #注意其中反向传播的计算 3 4 # -*- coding: utf-8 -*- 5 import numpy as np 6 import matplotlib.pyplot as plt 7 N = 100 # number of points per class 8 D = 2 # dimensionality 9 K = 3 # number of classes 10 X = np.zeros((N*K,D)) # da

AutoLayout代码布局使用大全—一种全新的布局思想

相信ios8出来之后,不少的ios程序员为了屏幕的适配而烦恼.相信不少的人都知道有AutoLayout 这么个玩意可以做屏幕适配,事实上,AutoLayout不仅仅只是一个为了多屏幕适配的工具, 它真正的意义所在是给了程序员一种全新的布局思想. 本文主要依据真实项目实例从三个方向全方位讲解AutoLayout的使用大全. 一.AutoLayout布局原理和语法 二.约束冲突和AutoLayout动画处理 三.AutoLayout布局思想,约束链的控制. 本文讲解的内容和代码主要依赖于一个名为UI

IOS AutoLayout 代码实现约束—VFL

在autolayout下,尽管使用IB来拖放控件,但仍然避免不了用代码来创建控件,这是约束需要代码来实现. IOS 提供了两种添加约束的方法 第一种: +(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multip

Where-To-Put-The-Auto-Layout-Code(AutoLayout代码应该放在哪里?)

Where-To-Put-The-Auto-Layout-Codehtml, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin: 0px; padding: 0px; height: auto; bottom: 0px; top: 0px; left: 0px; right: 0px; font-family: 'Helvetica Neue', Helvetica, Arial, sans

数据结构笔记01:线性表之顺序存储结构(ArrayList)

一般使用数组(C语言中的数组采用顺序存储方式.即连续地址存储)来描述. 优点:在于随机访问元素. 缺点:插入和和删除的时候,需要移动大量的元素. c语言实现代码:ArrayList 1 // Test.cpp : Defines the entry point for the console application. 2 // 3 4 #include "stdafx.h" 5 #include <stdio.h> 6 #include "stdlib.h&quo

Autolayout~代码实现

代码添加约束一般是四个步骤 1.创建需要约束的视图并且设置视图的translatesAutoresizingMaskIntoConstraints = NO(不设置这个约束不生效) 2.将视图添加到其父视图上 3.创建约束 4.添加约束 使用约束需要将视图的关系(添加到父视图或者其他视图树的关系)处理完成后在添加约束,否则有可能会试图错乱并且控制台会输出警告信息 代码添加约束是通过NSLayoutConstraint进行添加的, /* Create an array of constraints

《梦断代码》读后感01——Chandle的开始

故事的开始是在上个世纪的硅谷,那是软件的淘金时代,当时的人们带着各种各样新奇的想法投身计算机科技行业.就像故事开头一样,当时的科技行业就是一个Sumer——一个白板,一个轮廓,所有科技大牛都在上面添上自己浓墨重彩的一笔.但就像硬币的两面一样,有的兴盛,有的衰败,见惯了科技方面绝无仅有的创举,见惯了把科技历史拉近一大步的成功,但也有失败,那种无人问津的错误,使一个人跌入低谷的失足之举.我们在欣赏着软件史上的辉煌之时,一定要认识到这背后的种种辛酸困顿,<梦断代码>就是如此的一个故事,作者罗森伯格借