新聞中心
AutoLayout介紹
從網(wǎng)站建設到定制行業(yè)解決方案,為提供成都網(wǎng)站制作、成都做網(wǎng)站服務體系,各種行業(yè)企業(yè)客戶提供網(wǎng)站建設解決方案,助力業(yè)務快速發(fā)展。創(chuàng)新互聯(lián)建站將不斷加快創(chuàng)新步伐,提供優(yōu)質(zhì)的建站服務。
AutoLayout的功能要比AutoResizing強大的多。
當對一個UIView對象使用了AutoLayout布局后,意味著放棄了通過對象的frame進行修改視圖的位置、尺寸。
AutoLayout使約束條件,通過自動布局引擎,計算view對象的frame。
可以認為在AutoLayout中view對象的frame是一個只讀的屬性。
約束的核心公式:
view1.attr1 = (view2.attr2 * multiplier) + constraint
其中obj2可以是nil
除了=關系外,還可以是>= <=的關系
代碼適配
添加約束的步驟:
1)禁止被適配view的AutoResizing功能
- (BOOL)translatesAutoresizingMaskIntoConstraints - (void)setTranslatesAutoresizingMaskIntoConstraints:(BOOL)flag
2)創(chuàng)建約束對象NSLayoutConstraint
+ (instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c
參數(shù)即約束的核心公式
NSLayoutRelation枚舉:
enum { NSLayoutRelationLessThanOrEqual = -1, NSLayoutRelationEqual = 0, NSLayoutRelationGreaterThanOrEqual = 1, }; typedef NSInteger NSLayoutRelation;
NSLayoutAttribute枚舉:
typedef enum: NSInteger { NSLayoutAttributeLeft = 1, NSLayoutAttributeRight, NSLayoutAttributeTop, NSLayoutAttributeBottom, NSLayoutAttributeLeading, NSLayoutAttributeTrailing, NSLayoutAttributeWidth, NSLayoutAttributeHeight, NSLayoutAttributeCenterX, NSLayoutAttributeCenterY, NSLayoutAttributeBaseline, NSLayoutAttributeLastBaseline = NSLayoutAttributeBaseline, NSLayoutAttributeFirstBaseline, NSLayoutAttributeLeftMargin, NSLayoutAttributeRightMargin, NSLayoutAttributeTopMargin, NSLayoutAttributeBottomMargin, NSLayoutAttributeLeadingMargin, NSLayoutAttributeTrailingMargin, NSLayoutAttributeCenterXWithinMargins, NSLayoutAttributeCenterYWithinMargins, NSLayoutAttributeNotAnAttribute = 0} NSLayoutAttribute;
3)在UIView對象上添加約束對象
- (void)addConstraint:(NSLayoutConstraint *)constraint - (void)addConstraints:(NSArray *)constraints
將約束添加到哪個view對象上應按照以下規(guī)則:
對于同級view之間的約束關系,添加到它們的父控件上
對于不同級view之間的約束關系,添加到最近的共同父控件上
對于有層級關系的兩個view之間約束關系,添加到層次較高的的空間上
注意:約束不能重復添加,不能缺少必要的約束
添加約束的過程中非常容易出現(xiàn)無法計算出frame的情況
UIView的其他操作約束的方法:
- (NSArray *)constraints - (void)removeConstraint:(NSLayoutConstraint *)constraint - (void)removeConstraints:(NSArray *)constraints
AutoLayout的補充
AutoLayout的動畫:
代碼中如果修改了約束的數(shù)值,則執(zhí)行下面的代碼,就能產(chǎn)生相應的動畫效果。
[UIView animateWithDuration:1.0 animations:^{ [view layoutIfNeeded]; }];
哪個view中的約束變化了,哪個view對象調(diào)用layoutIfNeed方法
約束的變化應在動畫之前完成。
UILabel、UIButton這類顯示文字的控件使用AutoLayout的好處:
使用了恰當?shù)募s束,能夠使其尺寸自動匹配。
如設置了UILabel對象的上、左、右的規(guī)定邊距,則UILabel的尺寸會根據(jù)文字自動適應。
約束的VFL方式
Visual Format Language,可視化格式語言,是蘋果公司為了簡化AutoLayout的編碼而推出的抽象語言。
其實不能稱之為“語言”,可以認為這僅僅是一種“語法”,其目的是減少代碼使用AutoLayout的編程量
但實際減少的程度有限,有些約束的功能使用VFL也無法完成,但在實現(xiàn)一些簡單約束時非常有效。
NSLayoutConstraint的另一個創(chuàng)建方法:
+ (NSArray *)constraintsWithVisualFormat:(NSString *)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary *)metrics views:(NSDictionary*)views
format參數(shù):VFL語句
views參數(shù):VFL中出現(xiàn)的對象“鍵值對”
metrics:占位符
返回一組約束對象
VFL語句示意:
如:
canelButton寬72,acceptButton寬50,它們之間間距12
H:[cancelButton(72)]-12-[acceptButton(50)]
wideView寬度大于等于60point,該約束條件優(yōu)先級為700(優(yōu)先級大值為1000,優(yōu)先級越高的約束越先被滿足)
H:[wideView(>=60@700)]
豎直方向上,先有一個redBox,其下方緊接一個高度等于redBox高度的yellowBox
V:[redBox]-[yellowBox(==redBox)]
水平方向上,F(xiàn)ind距離父view左邊緣默認間隔寬度,之后是FindNext距離Find間隔默認寬度;再之后是寬度不小于20的FindField,它和FindNext以及父view右邊緣的間距都是默認寬度。(豎線“|” 表示superview的邊緣)
H:|-10-[Find]-[FindNext]-[FindField(>=20)]-|
ZXPAutoLayout
號稱最輕巧的自動布局,簡化了NSLayoutConstraint的繁瑣,采用新穎的鏈式語法,可擴展性強,維護成為低。
使用 zxp_addAutoLayout添加布局, 如:
[self.redView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) { }];
單個view的布局關系:
在superview中的內(nèi)邊距:
@property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^topSpace)(CGFloat value); @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^leftSpace)(CGFloat value); @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^bottomSpace)(CGFloat value); @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^rightSpace)(CGFloat value); @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^edgeInsets)(UIEdgeInsets insets);
如:
[self.redView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) { //layout.topSpace(20); //layout.bottomSpace(30); //layout.leftSpace(40); //layout.rightSpace(50); layout.edgeInsets(UIEdgeInsetsMake(20, 30, 40, 50)); }];
居中操作:
@property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^xCenterByView)(UIView *view,CGFloat value); @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^yCenterByView)(UIView *view,CGFloat value); @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^centerByView)(UIView *view,CGFloat value);
參數(shù)view應為superview
寬高操作:
@property (copy,nonatomic,readonly) ZXPAutoLayoutMaker *(^widthValue)(CGFloat value); @property (copy,nonatomic,readonly) ZXPAutoLayoutMaker *(^heightValue)(CGFloat value);
如:
[self.redView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) { layout.xCenterByView(self.view, 0); layout.yCenterByView(self.view, -100); layout.widthValue(100); layout.heightValue(100); }];
兩個view的布局關系:
//與另一個view的內(nèi)邊距相等 @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^edgeEqualTo)(UIView *view); //當前的top距離view為value點坐標距離 @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^topSpaceByView)(UIView *view,CGFloat value); @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^leftSpaceByView)(UIView *view,CGFloat value); @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^bottomSpaceByView)(UIView *view,CGFloat value); @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^rightSpaceByView)(UIView *view,CGFloat value); //當前top內(nèi)邊距值與view的相等 @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^topSpaceByView)(UIView *view,CGFloat value); @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^leftSpaceByView)(UIView *view,CGFloat value); @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^bottomSpaceByView)(UIView *view,CGFloat value); @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^rightSpaceByView)(UIView *view,CGFloat value); //與view等寬 @property (copy,nonatomic,readonly) ZXPAutoLayoutMaker *(^widthEqualTo)(UIView *view,CGFloat value); @property (copy,nonatomic,readonly) ZXPAutoLayoutMaker *(^heightEqualTo)(UIView *view, CGFloat value);
如:
[self.redView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) { layout.xCenterByView(self.view, 0); layout.yCenterByView(self.view, -100); layout.widthValue(100); layout.heightValue(100); }]; [self.blueView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) { layout.topSpaceByView(self.redView, 100); layout.heightEqualTo(self.redView, 0); layout.widthEqualTo(self.redView, 0); layout.leftSpaceEqualTo(self.redView, 0); }];
自適應操作:(對UILabel有效)
@property (copy,nonatomic,readonly) ZXPAutoLayoutMaker *(^autoHeight)(); @property (copy,nonatomic,readonly) ZXPAutoLayoutMaker *(^autoHeightByMin)(CGFloat value); @property (copy,nonatomic,readonly) ZXPAutoLayoutMaker *(^autoWidth)(); @property (copy,nonatomic,readonly) ZXPAutoLayoutMaker *(^autoWidthByMin)(CGFloat value);
其他操作:
@property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^priority)(UILayoutPriority priority); //優(yōu)先級 @property (copy, nonatomic, readonly) ZXPAutoLayoutMaker *(^multiplier)(CGFloat multiplier); //約束的倍數(shù)
如:
[self.blueView zxp_addConstraints:^(ZXPAutoLayoutMaker *layout) { layout.topSpaceByView(self.redView, 100); layout.heightEqualTo(self.redView, 0).multiplier(0.5); layout.widthEqualTo(self.redView, 0); layout.leftSpaceEqualTo(self.redView, 0); }];
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享名稱:UIKit框架(8)屏幕適配(二)-創(chuàng)新互聯(lián)
文章路徑:http://www.ef60e0e.cn/article/dpchjd.html