Learn to love Auto Layout programmaticallyWord格式文档下载.docx

上传人:b****5 文档编号:8410087 上传时间:2023-05-11 格式:DOCX 页数:24 大小:569.21KB
下载 相关 举报
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第1页
第1页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第2页
第2页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第3页
第3页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第4页
第4页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第5页
第5页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第6页
第6页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第7页
第7页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第8页
第8页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第9页
第9页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第10页
第10页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第11页
第11页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第12页
第12页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第13页
第13页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第14页
第14页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第15页
第15页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第16页
第16页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第17页
第17页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第18页
第18页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第19页
第19页 / 共24页
Learn to love Auto Layout programmaticallyWord格式文档下载.docx_第20页
第20页 / 共24页
亲,该文档总共24页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

Learn to love Auto Layout programmaticallyWord格式文档下载.docx

《Learn to love Auto Layout programmaticallyWord格式文档下载.docx》由会员分享,可在线阅读,更多相关《Learn to love Auto Layout programmaticallyWord格式文档下载.docx(24页珍藏版)》请在冰点文库上搜索。

Learn to love Auto Layout programmaticallyWord格式文档下载.docx

BeforeopeningxCodelet’squicklyintroducethe 

VisualFormatLanguage(VFL)andthefunctionsneededtomanagethelayout.

WithVFLyoucandefineconstraintsusingasimplesyntax.Forexampleyoucandefinethe 

width 

ofanelementusingthisstring:

"

H:

[element(100)]"

ortheheightusing:

V:

Thefirstuppercasechartellswhichdimensionyouwanttomodify. 

standsforhorizontaland 

forVertical.Thenyoudefinetheconstraints(moreaboutthatinthenextexamples).

Constraintsaredefinedbythe 

NSLayoutConstraint 

class.YoucanattachnewconstraintstoaviewwithUIView’smethod 

addConstraint(s):

 

andremovethemwithremoveConstraint(s):

Let’scode

Download 

theproject 

andopenthefileviewController.m. 

Thisfilecontainsallthecodeforthistutorialanditisorganisedinsingleautonomousfunctions(withalotofrepeatedcode!

F@*&

youDRY-.-).Eachfunctionrepresentsanexample. 

Youcanactivatetheexamplesfromthe 

viewDidLoad 

function.

Themainviewcontains2subviews:

redViewandyellowView.InthenextexamplesyouaregoingtoplacetheseviewsintotheboundsofthemainviewusingAutoLayoutonly. 

Themethods 

setupViews 

istheplacewheretheviewsareinitialisedandconfigured:

-(void)setupViews

{

self.redView=[UIViewnew];

self.redView.translatesAutoresizingMaskIntoConstraints=NO;

self.redView.backgroundColor=[UIColorcolorWithRed:

0.95green:

0.47blue:

0.48alpha:

1.0];

self.yellowView=[UIViewnew];

self.yellowView.translatesAutoresizingMaskIntoConstraints=NO;

self.yellowView.backgroundColor=[UIColorcolorWithRed:

1.00green:

0.83blue:

0.58alpha:

[self.viewaddSubview:

self.redView];

self.yellowView];

}

Reallyimportant:

whenyouhavetodealwithAutoLayoutprogrammaticallyyoushouldturnoff 

translatesAutoresizingMaskIntoConstraints 

.Thisensuresnoconstraintwillbecreatedautomaticallyfortheview,otherwise,anyconstraintyousetislikelytoconflictwithautoresizingconstraints(whenyouaddconstraintsfromIBitautomaticallysetsthatpropertyto 

NO 

).

EX1:

SimpleconstraintswithVFL

Great!

youarereadytogetyourhandsdirty. 

Gotofunction 

example_1 

.Thiscodejustaddsasquareinthetopleftcornerofthemainview,likeinthenextimage.

1. 

First,createadictionarythatassociateskeystoanyviewthatyouaregoingtouseinVFLdefinitions.

NSDictionary*viewsDictionary=@{@"

redView"

:

self.redView};

Inthiscasewe’vemarkedareferencetotheredViewviewusingthekey“redView”.

2. 

TimetocreatethefirstconstraintswiththemethodconstraintsWithVisualFormat:

options:

metrics:

views:

ofNSLayoutConstraintclass.

ThismethodreturnsanNSArrayofconstraints.DependingontheVFLyoupasstothefunctionitcreatesoneoremoreconstraints.

InthisexampleweshapewidthandheightconstraintsforredViewusingthiscode:

NSArray*constraint_H=[NSLayoutConstraintconstraintsWithVisualFormat:

@"

[redView(100)]"

options:

0metrics:

nilviews:

viewsDictionary];

NSArray*constraint_V=[NSLayoutConstraintconstraintsWithVisualFormat:

Let’sstartbyanalysingthevisualformatstrings. 

Aspreviouslyshown,VFLusesHorVtodefinetheorientationoftheconstraints.Thenthesquarebracketsencloseareferencetoaviewandtheparenthesescontainsthevaluefortheattributeswearesetting(dependingonVorHwesetwidthorheight). 

Weuse“redView”topointouttheredViewviewbecausewehavepreviouslydefinedakeyforitinviewsDictionaryandwepassittothe 

argumentofthefunctions.

NowthatwehavedefinedconstraintsforthesizeweattachittotheredView:

[self.redViewaddConstraints:

constraint_H];

constraint_V];

3. 

TocorrectlydefinetheconstraintsforaviewyouneedtogivetoAutoLayoutenoughinformationtoobtainsizeandposition.WehavealreadygiveninformationforthesizeofredView,nowlet’splaceitinthemainviewbounds.Again,weusethepreviousfunctionjustchangingthevisualformatstring.

NSArray*constraint_POS_V=[NSLayoutConstraintconstraintsWithVisualFormat:

|-30-[redView]"

NSArray*constraint_POS_H=[NSLayoutConstraintconstraintsWithVisualFormat:

|-20-[redView]"

Let’stranslatethefirstVisualformatstringinasimpleEnglishsentence.@”H:

|-30-[redView]”standsfor“RedViewmustmaintainadistanceof30pointsfromtheleftsideofitssuperview” 

Whilewritingthestringthisway@”H:

[redView]-30-|”wesaythedistancehastobecalculatedfromtherightsideofthesuperview. 

Thepipechar“|”canbereadasareferencetothe“superview”oftheobjectbetweensquarebrackets.

ThesameistruefortheVerticalorientation,thistimeapipeontheleftmeans“top”whileontherightmeans“bottom”side,sowewrite:

andthiscodeisdefiningthattheredViewmustkeepadistanceof30pointsfromthetopsideofitssuperview.

YoucouldalsoletthesystemusedefaultspacingcreatingaVFLstringlikethis:

|-[redView]"

Nonumericinfoaregiveninthisstring.iOSwilluseadefaultdistance.Justrememberthatthesyntaxneedsthe“-”toseparatethepipeandtheelement.

Nowweneedtoattachtheselastconstraintstoaviewtoletthemtakeeffect.

Theviewtoreceivethisconstraintsisthe 

mainview 

.Attentionthough,wearenotattachingtheconstraintstotheredView.Toeasilyrememberhowtoattachconstraints,justrememberthatitisresponsibilityoftheparentviewtoassignpositiontoitschildren.Sointhiscase,themainviewreceivestheconstraintstoarrangeredViewwithinitsbounds.

[self.viewaddConstraints:

constraint_POS_V];

constraint_POS_H];

Ifyouareusedtoplaceviewsusingtheframepropertyitcouldtakeawhilegetusedtoit.

Atthispoint,compileandruntoyouobtainthepreviouslyshownresult.

EX2:

SimpleconstraintswithVFLandmultipleviews

FromviewDidLoadcommentexample_1andremovethecommentsfromexample_2function.

Inthisexamplewearedealingwithtwoviewsandwecreateconstraintswhichworkforboth.Hereisthefinalresult:

Step 

and 

areidenticaltothepreviousexample.HerewesettheviewsDictionaryanddefinesizeconstraints.ThistimeweneedtoaddinformationfortheyellowViewtoo:

NSArray*yellow_constraint_H=[NSLayoutConstraintconstraintsWithVisualFormat:

[yellowView(200)]"

NSArray*yellow_constraint_V=[NSLayoutConstraintconstraintsWithVisualFormat:

[yellowView(100)]"

nil

views:

[self.yellowViewaddConstraints:

yellow_constraint_H];

yellow_constraint_V];

AsItoldyou,aparentviewhastotakecareofitschildren’spositions.Sowecreateconstraintsforthemainviewtodefinewhereredandyellowviewhavetobedrawn.

Let’sstartbywritingtheHorizontalinformation:

|-20-[redView]-10-[yellowView]"

Weareusingthesamefunction,sojustfocusontheVFLstring 

@”H:

|-20-[redView]-10-[yellowView]“ 

.Translatingittoplainenglishwe’llendupwithmorethanoneconstraint:

-redViewhasadistanceof20pointsfromtheleftsideofitssuperview 

-theleftsideofyellowViewis10pointsdistantfromtherightsideofredView

Doesitmakesense?

Iordertohelpyoureadthesestringsyoushouldkeepinmindthattheyarebasedonreallysimplesequenceswherethemainactorsarethe“|”andtheviewsbetween“[]“.Alltheinformationthatseparatestheactorscreateconstraints.

Andtobuildverticalinformationwewrite:

|-30-[redView]-40-[yellowView]"

Thisstringsays 

-redViewhasadistanceof30pointsfromthetopsideofitssuperview 

-thetopsideofyellowViewis40pointsdistantfromthebottomviewofredView.

Again,weattachtheconstraintstothemainview.

EX3:

SimpleconstraintswithVFLusingalignmentoptions

Thepreviousexampleplacestheviewsinamessyway.Let’srearrangetheminacleanerlayout.Forexamplewecouldaligntheviewstotheirtopsidesobtainingthisresult:

Achievingthatissurprisinglyeasy!

WejustcallthefunctionsconstraintsWithVisualFormat:

views 

specifyingavaluefortheoptionsparameter.Infactthisparameterstandsforalignment-options(atleastatthemoment).

NSArray*

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 成人教育 > 成考

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2