Cartography-Swift的自动布局第三方库(官方文档翻译)

Cartography-Swift的自动布局第三方库(官方文档翻译),第1张

概述源地址:https://github.com/robb/Cartography 使用Cartography,你可以抛开链式语法,使用声明式的代码设置你的自动布局约束! 简而言之,他可以让你将如下的代码: addConstraint(NSLayoutConstraint( item: button1, attribute: .Right, relatedBy: .Equal,

源地址:https://github.com/robb/Cartography

使用Cartography,你可以抛开链式语法,使用声明式的代码设置你的自动布局约束!

简而言之,他可以让你将如下的代码:

addConstraint(NSLayoutConstraint(    item: button1,attribute: .Right,relatedBy: .Equal,toItem: button2,attribute: .left,multiplIEr: 1.0,constant: -12.0))

替换为以下的代码

layout(button1,button2){ button1,button2 in    button1.right == button2.left - 12

如果你在使用Cartography的过程中遇到困难,可以联系作者 [Twitter] 或者 [email] .

用法

调用@H_403_17@layout方法,传入@H_403_17@UIVIEw或@H_403_17@NSVIEw类型实例,在闭包中声明各个视图的不同属性之间的约束:

layout(vIEw1,vIEw2) { vIEw1,vIEw2 in    vIEw1.wIDth   == (vIEw1.supervIEw!.wIDth - 50) * 0.5    vIEw2.wIDth   == vIEw1.wIDth - 50    vIEw1.height  == 40    vIEw2.height  == vIEw1.height    vIEw1.centerX == vIEw1.supervIEw!.centerX    vIEw2.centerX == vIEw1.centerX    vIEw1.top >= vIEw1.supervIEw!.top + 20    vIEw2.top == vIEw1.bottom + 20}

对于每个等式或不等式左边的vIEw,Cartography会自动将其@H_403_17@translatesautoresizingMaskIntoConstraints属性设置为@H_403_17@false。如果这个vIEw不是受你控制的----比如它是属于一个苹果提供的@H_403_17@UIVIEwController类----你需要在声明约束时对其保持适当的关注。

要注意的是,@H_403_17@layout在必要的情况下会对自动对视图进行重新布局。如果你想要自己控制这个布局的步骤的话,你可以用@H_403_17@constrain方法来代替它:

constrain(vIEw1) { vIEw1 in    vIEw1.wIDth  == 100    vIEw1.height == 100    vIEw1.center == vIEw.supervIEw!.center}UIVIEw.animateWithDuration(0.5,animations: vIEw1.layoutIfNeeded)
约束的替换

你可以获取一个约束组(group)内的多种约束,随后用新的一些约束来替换它们。

constrain(vIEw) { vIEw in    vIEw.wIDth  == 100    vIEw.height == 100}// 获取约束组let group = ConstraintGroup()// 使'vIEw'关联到它的父视图的左上角 constrain(vIEw,replace: group) { vIEw in    vIEw.top  == vIEw.supervIEw!.top    vIEw.left == vIEw.supervIEw!.left}/* 之后 */// 将这个vIEw移动到它的父视图的右下角constrain(vIEw,replace: group) { vIEw in    vIEw.bottom == vIEw.supervIEw!.bottom    vIEw.right  == vIEw.supervIEw!.right}UIVIEw.animateWithDuration(0.5,animations: vIEw.layoutIfNeeded)

为了方便起见,@H_403_17@layout和@H_403_17@constrain方法都会返回@H_403_17@ConstraintGroup实例:

let group = layout(button) { button in    button.wIDth  == 100    button.height == 400}
支持的属性

Cartography支持所有iOS 8和 OS X 10.9内置的属性,如下:

@H_403_17@wIDth

@H_403_17@height

@H_403_17@top

@H_403_17@right

@H_403_17@bottom

@H_403_17@left

@H_403_17@leading

@H_403_17@trailing

@H_403_17@centerX

@H_403_17@centerY

@H_403_17@baseline

以及iOS的特性

@H_403_17@firstBaseline

@H_403_17@leftmargin

@H_403_17@rightmargin

@H_403_17@topmargin

@H_403_17@bottommargin

@H_403_17@leadingmargin

@H_403_17@trailingmargin

@H_403_17@centerXWithinmargins

@H_403_17@centerYWithinmargins

@H_403_17@edgesWithinmargins

这些属性都可以通过以下运算符来进一步细化:@H_403_17@*,@H_403_17@/,@H_403_17@+ and
@H_403_17@-。

此外,它提供了一种方便的方法,支持复合属性的设置,因此你可以一次分配多个属性。

layout(vIEw) { vIEw in    vIEw.size   == vIEw.supervIEw!.size / 2    vIEw.center == vIEw.supervIEw!.center}
layout(vIEw) { vIEw in    vIEw.edges == inset(vIEw.supervIEw!.edges,20,40,20)}
多个视图对齐

如果你需要让多个视图对齐一个共有的边缘,你可以使用@H_403_17@align方法。

layout(vIEw1,vIEw2,vIEw3) { vIEw1,vIEw3 in    align(top: vIEw1,vIEw3)}

这相当于@[email protected] == vIEw2.top; vIEw2.top == vIEw3.top。 还可以对@H_403_17@top,@H_403_17@right @H_403_17@bottom,@H_403_17@left,@H_403_17@leading,@H_403_17@trailing,
@H_403_17@centerX,@H_403_17@centerY 和 @H_403_17@baseline进行相似的设置.

视图均匀分布

为了让视图均匀分布,无论是水平的还是垂直的,我们都可以调用@H_403_17@distribute 函数:

layout(vIEw1,vIEw3 in    distribute(by: 10,horizontally: vIEw1,vIEw3)}

这相当于 @[email protected] == vIEw2.leading - 10; vIEw2.trailing == vIEw3.leading - 10.

设置优先级

你可以通过@H_403_17@~ *** 作符来设置约束的优先级:

layout(vIEw) { vIEw in    vIEw.wIDth  >= 200 ~ 100    vIEw.height >= 200 ~ 100}
获取约束

由于@H_403_17@==,@H_403_17@>=,@H_403_17@<= 和 @H_403_17@~ 生成了 @H_403_17@NSLayoutConstraint 实例,如果你在之后需要用到生成的结果的话,你可以用如下的方法来获取它:

var wIDth: NSLayoutConstraint?layout(vIEw) { vIEw in    wIDth = (vIEw.wIDth == 200 ~ 100)}

要注意的是,复合属性的声明一次返回多个约束:

var constraints: [NSLayoutConstraint]?layout(vIEw) { vIEw in    constraints = (vIEw.size == vIEw.supervIEw!.size ~ 100)}
文档

在这里可以查看文档。 更多的信息可以在gh-pages 查看。

支持

如果你有疑问的话,别犹豫,直接提交。

关于Cartography

Cartography由Robb Böhnke创建并受Florian Kugler的[FLKautoLayout]项目启发。

总结

以上是内存溢出为你收集整理的Cartography-Swift的自动布局第三方库(官方文档翻译)全部内容,希望文章能够帮你解决Cartography-Swift的自动布局第三方库(官方文档翻译)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1085135.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存