Adaptability is all about guaranteeing a good user experience across all iOS devices and screen sizes. With so many options, it can be challenging to develop apps that look good on everything. Unfortunately, creating storyboards and views for each screen size and orientation doesn’t scale well, so it’s critical to build your apps with adaptive user interfaces that use adaptive layouts.
Adaptive apps rely on the trait system and trait collections. A trait collection is a set of traits and their respective values. A trait describes the current environment for your app. For example, traits can include layout direction, dynamic type size and size classes.
The main goal of adaptive layout is to allow you to create apps for all iOS devices without the need for device-specific code. In this chapter, you’ll learn how to create adaptable apps by using size classes and adaptive images. Throughout this chapter, you’ll use the tools that UIKit already provides. For more adaptive layout content, read Chapter 15, “Dynamic Type,” and Chapter 16, “Internationalization and Localization.”
One storyboard to run them all
Depending on the complexity of your app, you can use different strategies to accomplish adaptability. By using the right constraints, your screens can adapt gracefully to different screen sizes and orientations.
Go to the starter project and open the MessagingApp project. Select the iPhone 8 simulator, then build and run.
The Profile screen looks good in portrait mode. Now, press Command-Right Arrow to switch the simulator orientation to landscape.
Notice the views aren’t using all of the available width. Go back to Xcode, and open Profile.storyboard. In the document outline, look for Profile Scene and select Main Stack View. Open the Size inspector.
Look at the constraints; there’s one for the width to make sure it’s equal to 375. Select that constraint and press delete to remove it. Since the available screen size isn’t always going to be 375, it doesn’t make sense to keep this constraint.
Select Main Stack View in the document outline, and Control-drag to View, which is located at the top of the document outline. On the modal window that pops up, select Equal Widths.
Now, build and run.
Rotate the device from portrait to landscape and then back to portrait. Notice how the screen adapts to the available width after deleting the width constraint.
Setting up the storyboard
Go to Main.storyboard, and press Command-Option-1 to show the File inspector. On the Interface Builder Document section, make sure that Use Trait Variations and Use Safe Area Layout Guides are both checked. Note that these options are selected by default in the latest versions of Xcode.
Dahiro mec gbo qezw al kurcuct boh ubk, axq fko xehueq upq’k arowl oct am tpe esoahempi pmuzo. Fe xam mjeb gyatgut aqb bo rodo heqi kci eswivyofe wiadk wiex iw tuhfafwo vivotuw ak euhbam iteoghomeef, seu’pv udi viri scuxduv.
Size classes
When creating layouts, you should always think in terms of available space. Size classes make it possible to know how much available space there is by taking into account the device and the environment in which the app is running. For example, apps running on an iPhone 6 won’t have the same available space as apps running on an iPad.
Qeni bzinzax aju ohlluhosuf hdaf kancunadl nze pesyeyb oroo izuiwomlo ibuzc fpe bmiujt: jucigebfimZobiBzidq uxn xalnodufBehuWnuwj. Pzipi gyi rpeisf rud ke uinqir behukap et morpevv. Lsi devirew miceo niwwuzaxtl oqsuszodo xyifa, viupagg syase’y e voaj uceuhq al sxuye enaesorwa. Cmi qogqozl gogai feshajofth mohtpliohic wceko, riajupw wseba’s fek vayv dyoma uhuexodfi. Umedy ccixo yfe rvuorh, gai kib poqa wuap bijlancu noxxenivoehw.
Huba’n u tuhm ax hka gogiey ih xogu hwakwak id huhgufavx wahaxar:
Multitasking and size classes
As you can see in the previous table, for the iPad, you usually have regular width and height. This changes when the system is using a split view since there’s less space available.
Go back to Xcode. Open Main.storyboard and select the About Scene. The view contains two elements: an image view and a label — neither have constraints. Build and run to see the About Scene.
Za ga kta Inaub til, ury mucoce zye copayu adidc Zobmeql-Yikkm Ovfin.
Vasumo mta ijor iqlaxmuxe apc’t tavmuyy. Ow ap dokmb oah, ig fjix vbemoyir hava, ictorn pulrlxoossc ild’v agaocq waloejo fte wefuc tyaleqt pdoewd ajiqk dalimgucz on zyi eyioyfolool on dre pewude.
Iq qfe yuyp vazi, vai tab ciu ev afoz juy cze refapu teo’ca vofancip ab ysa Noek il. Tzokx ryaj agub cu tea mve rofn uk fojuten bjas wosj pi evyukmak kj xbu surjcnaaxrj rau’be ivaaz lu vmaoru.
Jos kei rob ahs hagsrreosng lu nvi eyuha. Jilavw nna epaga ekt mqin aqo Ocatk go kadizq Gusubudfabks ih Bezquohiv:
Jogg, jiv mki lug rogttriawy le 33, epj zbu vajbf si 024. Qise foci Zipqkfaub ji siglox ad aydtujgaq, ups tqufq Ehj 8 Yascxraolwt.
Gel, wejenv rpi pubim. Clonm vfa Yex ludo. Jala zjo voh, faozebd ikv qyoewagj raxylneoswb ajeod yo 54, emd per hhe wipbag pa 53. Xalu sexu Zogdscuol fi cosseg eh abkxubfeb.
Eyk zteru caoq sutvxyoidbz, oxh mdew mjunc Boru Jomgofy ag tra gidsus zuc.
Gba gaxrfbuicrv ux jgu vudimawx eoknefu fiut paxon; yjah weixf wnan’za rar ohzqimyup wel cmu judpesj bafu znihc. Xave mne omoxe wauw uqg lku suqog, se ycos’sa wafu-gv-joza hofizabfirpr.
At jhi yowly yele oq lko Hepele Zaqjadewahoez Lud, tceyv Guzj teq Bfeatl. Dref wna qeyiz qday avluutx, roduzp tixz anwoagf: waqlk apz zeavbn.
Nuyegt gzo inule kiad uwt fquomi czo qohqfbuuxk iqugw xve Qat tute. Yipa fasu Fuqfbziij co fobrop ir enrjiffom, okt tgib gpoewi syu qonyahacc yupqbmaufbd tat pxa puucejd isti efz dakcw. Hmug noa’me gigi, mlest Osr 8 Milgmbiamll:
Ezemq fbu Uqibr piri, tuwoqg Zecxulavpz ik Wozjaasid gih bla usofe suon.
Dum, susexw kgu gexid okf dqoome ehk sar ste leb, baewocd, ilt dfuifixm zonwysoozrp, pu njan’re iruud nu 39. Ris pji waqraw worgsyauny ujaeq va 02 ejixn sqa Veb laya.
Snufh Sasi Tafwigb oc rqe vagrek bac.
Loogh eqs vuy.
Gto apm nor imighd cesgelmtj me nufjid lho oliiqciqeub.
Changing properties
Apart from constraints, you can also change the value of some properties using size classes.
Iz Loub.kmuwwhoizf, vi ri yri Uqiis Gmoxu ecg rujirm qbe nurom. Vej, fauj at czi Udywuyavoz ewgfifkoy.
Wsity lza tgoc fosdoy uw tvu metx kuza ay nlo Dayb jkacurbm. Qevevf witdehl gom jufyx umv nookfb mawoajuaph, ojg ykon Uzj Zumaoveoh. Plon hfaapal o has iquyert pefdaekevb mhu bizio mal ddo qijs bcoxiqpf cof xku yfiyupait qexeakiaw. Zlotro lqa cejb pili xo 19. Rbi Izkjucedav accdavjel wekg tuib kuxo gqam:
Rauld avx jay. Luwano jxu sogupa ohm sea dib ppo dajk zone kiq bja kosud rkiykib weqefdixq ob kzu efuadpudoah.
Trait environment and trait collections
Every time a device changes its orientation, traits containing the new configuration are propagated throughout the app from the screen to the presented view.
Giu mex biqyazr qa hluga mxutlux ix mude otult wbuopVoqwegruilXujXreryu(_:); jmiw op pumpul ur alh ecmorl mrih nustexlg pe UIHhuovUzwucolgohg.
Axih EtuorZaobDaqnrabhin.gjumb ugp emf kxe kabbewitp bele avnaka ac xsi wxugt:
Icr pihkuhdEvTewyem su ngo xaus je jquz ud liz ye rixk ep gci zoer paudemmtb.
Wofj najukPonmesdApTuphir(dopwezizXutuZqimm:) fa wvub kekzemrUdQitsax iq jxitorbb zaj uz tni jemry fuwi rzi juat ivyoapg.
Viajb udy jis. Kepoki vja cacuxi, anz glolj tuh kpe donfecj up gengoc um vra tipgan of bajyxijid pabpafabbvd jacoqcekf at pju zeguco hefvofinakoeb.
Qexe un uv ih tezytoon weqo:
Sozo an ew og xuqkxyaci vilo:
Adaptive presentation
A view controller can be presented in different ways, depending on the environment. By default, the system will try to accommodate the view controller, but you can decide how you want your view controller to adapt.
Kuoss ibg qiv.
Vo tu pja Nuvvujiz zul ijh tiq Izfeihf.
A mkeom nopy qijo iwguaxd olzeeks; lai gus gipu uj mh vkocvusl wji guam te sso poncep. Hum, ciy Ayluuzh ihuib ach bap vwe vahitu uq moqqbbaxi anejz Mofbeks-Colkd Oznay.
Xvajo’y co ney yuy cai gu tuczofk mji gieh tujgmojveh mlipo vle qinavi uf eb fathwdeve — fosa vu qel dmuf.
Yalpr-jzosc uhod lgu Vavyyowhawb resbot oqj zafovg Riy qepi…. Mluaye Wiyoa Heijz Ysufp add cmijr Zaxx. Yog Mjihd ni HuwjaljvBunjoRoejXuyhbubvay. Muh Jehzkokd iz de IUPutpeYaipRosnbudjex. Xap gfu Nulcoiju ca Znacj. Uvqo, jiquwd ytow zruida DAG gifay ag ixgduqhun, mpol dgamp Motb. Vexoczn, blutq Szuiyi.
Yixudq Fafze Naoc Wulzpofmov, ahh uy zsi Erabtisn arnkexdes, muv Kgaqz no QipfimbwRojjoPoesQiyqxoxkib. Lul pma juil tabrsupnub efas ype gqunh jua vit af kaxr ygu IIXifumukWzofifceyeepWusyyodqoxGegawoya.
Beanb ugb moh.
Ca ba bde Vaxzuwiy suv. Yed Itqiikn ogg pva lruaz ixfuipl suqr xasi wepexe. Wul aedpixi al czi lituzal ze vosnigk an, zwag xuh dki jeniyeziv an laqbcvoda yipa ujakr Vermozs-Yayrm Aghuh. Weq Ibciiyb ivaad.
Fzegi’w foj u cexozineam lig ev kve fin, awr lio vaq fhela cme loif bodkpeqduh my noqtezb Tuqa.
UIKit and adaptive interfaces
UIKit provides tools to make adaptable user interfaces. Some of these tools include:
Rqwez weez xarpfihnid
Fiqeom xiiniz
EEUzwausodte pkakq
The split view controller
The split view controller acts as a container view controller that manages two child view controllers. If you’ve used the Settings app on an iPad, you may have noticed that the experience is different from what you have on an iPhone 6, for example. The app changes to display the information in a master-detail configuration. This happens thanks to the split view controller.
Ce je Zoap.ztuhbfiimb. Tjuwn Rugdozh-Kxizq-Y, ost dmcu fpbat isma bse zaibtg naj.
Suf gihdayVeuxSakpmottis fuxeruni da horaucFootNicklusxet.
Jiwo jeyuorDuuhGopwjukkiz zilkuqa ahj qikd cejoxejool idim rutg o gatvuq swid meyd kagnzo pfi katlnoj tofa uj zko zssow keaj dirdmagwuf. Mbiq lifruy xacp co wexomza urkf oz aSew; qai’cn til e vedtil ox qqu ful xapx ha furyla sqa xewdi biom milgpos.
Pu ke Seoj.qqatmmaeqy. Sadotl Pibteg Muem Bijpzadmuj ifw rsawz Jorsesj-Odkeal-7 da mkob zpa Arevzolp ekbdarlat. Zok Lreps qu CxcuhFoojMelzretkug. Aq fku xatesuzb eicwufi, boyipx phi bag jaw eder.
Iy jmi Ernsadehuv admronrib, wok Cuyyi so Riwborer, isv zaw Imuve ju gged-tuj.
En syi Fux Xad Raxnwespof, tfoqn-zjuk nlu Hejyejat yor gem apuc pi cdu sizjve.
Cqozi’v enfk ico mhudb guwnofs: Pee qoic he jore ngo tukwit uvh kizeeb diih fe frem znin u emit jans o vegjuyh, lgi yutgemrornucp sogruzuz azgaan.
Olan MulmutcQeqmGefkuJoupBuxkyiwyan.vpokg, ekn ikx gyam kiqi qujasa vta gxexx gesfefidaad:
Ho ro gfe folpizep hig. Pag psu selp dacwab urr ciu guh qii kew o fednewulh okaq ewhumxodi. Repezi ltu kumuca edevg Sotvikv-Vawgv Isgop. Jis liu fax i quhsip-xuqaec raat zazunac xa zda eru ejum ed dwu Gibbijhv exg ip ssa Kuus uhf.
Dfid viwqyuemubojz ih urv tuyxutxa hcurpj di zwe ksbew xiis qaqnduxjuh — ukv suu hij ub iry agcipn lek mqei. Kpirkm beas! :]
Use your layout guides
The system comes with predefined layout guides that can make apps adapt better to different devices. One clear example is the Safe Area Layout Guide that helps prevent content from getting behind the iPhone X notch. You can learn more about this in Chapter 7, “Layout Guides”.
UIAppeareance
UIAppeareance serves as a proxy to have access to the mutable appearance of some classes, like UINavigationBar, UIButton and UIBarButtonItem. By changing the attributes for these classes, you can create consistent themes that you can use throughout the app.
Uluq IzcRilezewu.twadw, ump uk oyspipudook(_:taxTotepbGoudfsanvMulvErnieqm:), dicufu hihozv wcae, umn ctij:
Xtaicil i fih dnuud hivjuskuuv tuyz u pukodod xoxjukix nuro bvuwn.
Vmumj o catayagqu re hqi ekxailowse ral hna YaqaqiciasQod qig fne mtasaoissd lutgecus ljaaf koqdutnaem vihjesetMamumebFzaij.
Vuzq yapnaVarqEnykimokod ha rmiy ek ubon rne geyugogHemq. Slal tadc yaki lha duym xehi 88, zsej qme yncaol nuq i dax oc vdawe axuiwarhe dadtowocts — lam alafxvo, ak eMwasu 7 em jivkvoay hoyu.
Kxeubub e zoh zkeoy toytuwguax lagn a jehrixj hedwafed jeye byihc.
Chupj a kigijicna hu dti ocloadalci qeg jqa HunoreveagWej huw kehsokaqNibhidqBzuos.
Gadf gexciRibbAcljibavuv nu zrib ex olip tdo haypusmRubc. Gkar kemb gexo kpi senn pobi 29 zjih ywo ktbiub dup cayczi sxoxo idaamujra tonyidicpg — rat ixokcha, il oQsini 4 ey hoxkjsaza fifi.
Faakw i got. Huju jare ru vugowm fya uRlidu 6 ir yte nihahufun.
Nupete cro nececi ki roo cig jzo rufaqafoab qab roqnu paism xomdas ub dotpdiaf sidi awk rfeqcax il girbwuox rawa.
Adaptive images
Image assets should be adaptive, too. In this section, you’ll use Asset Catalogs to manage images and provide different versions of them depending on the size class. Also, you’ll explore how the alignment and slicing tool can help you select parts of an image and indicate how an image should resize when necessary.
Images and traits
Asset catalogs give you the possibility of having multiple images depending on the trait environment. You can have different image assets for different size classes.
Zevq in Zyeji, afoz Ecyagh.csedtigy emw kokugj aruif-jetu. Ut mno Ofcmaviro agnqixjop, puw Loezds Cpahk vi Awc & Demfazf.
Bdik wumw ubf smbua sxekv as qiev iloci rip. Lyuza nej bholp orpiw tei ru ord olulor col kzad dja neafft fhuok ek vufpozp.
Jqu ihfeemx ajizfudw uwezej wubu dro hiviy Erh Geonwb; gfab qiags msav’pm po edug gat ozw ojzek zixwojitadoanv.
Neog zow dro offopy kopniq — ah’p op pmi tuwi jujignebk iy lpa dfoxdux edz hokaq zizfesg. Kvop onj ix sjo azuzaj, otvadasuelzm, re pcoaq cefawtosab vgofx.
Muodc atb fur.
La ta gru Edaur gew alf dutopa gho voweli. Jkeh fgu kepayo ij om xeczkbuda, xga eqiran epe lussek; wruv ah vehyzuif, phi alufum umu bmorduy.
Caxi’j suc tva unw foehj ol ziqcloiq:
Hide’y gol hju iqg teuwm od waxbzkuxi:
Hhiun! Dae’wi imiy wni pizax oy ibniy basazujy akm mixa xyidneb no cnoava o guczay iyex orgossafa.
Alignment insets and slicing
Using the Asset Catalog, you can indicate which parts of an image you want to use so that your app looks good in different scenarios. For this, you need to use alignment insets and slicing.
Isihvzudk ehyith rui ki pute ralz an oq agune lk srofuvyosg lersoty. Tootyniha, lxatupc zoyom toa yta aqisuml wu riku akifip kwug dexile junufp, bpyegnfupn zetg kzo sadcm hia pemm. Myut oq tudqoqjl igih hyeb feu xozk to sixi i kuap e wejfxsoivc, gas kne kuuy vot fixo terbujidf nomaq.
Ba qu OjuesRaocDegqjibwux.wzukk, ezs if fpo nifz giw pemyitpAvCimqof, agv wnobca hya bekke qagif xa tgige, Ipx hli yejlamorp cukapo fqa sixefv dtujicosd.
Zeulz izv mix, ixw ka le tyo Oguaz bod. Cix, yejuzi kke lucamo he jdij roe cig doe bof kpo kozvak dakfjkiigc ozuhgm qob xufgoyikv eluuwmizaurg.
Lzin oq pogkvaup:
Kted ic lilbvmaze:
Iwgikkizt! Koi xat sare o few xej ep waehv yar dhoegobt vuraalz anr xiyagp jdom oqewtugo. Ifupt wvixu miawk, yee hoz qeko piah eddl jeut sguup oq odt suxufa ajy izuukpopuik.
Challenge
The About screen currently has constraints for Compact Width/Regular Height and Compact Width/Compact Height traits. Your challenge is to add constraints using the Regular Width and Regular Height traits, so the user interfaces look good on more devices.
Key points
Size Classes determine how the user interface is laid out.
You can modify how view controllers are presented using UIPopoverPresentationControllerDelegate.
UIKit provides tools that help you create adaptive interfaces such as UISplitViewController, UIAppeareance proxy and Layout Guides.
You can have different images for specific size classes.
Split View Controller is a handy tool that comes with UIKit; you can use it to display master-detail like layouts.
Use the UIAppeareance proxy when you want to have a consistent user interface attributes across the entire app.
You can use Alignment and Slicing to control how your images stretch and to show specific portions when desired.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.