The content-hugging and compression-resistance priorities (CHCR priorities) define how a view behaves when constraints that are anchored to it attempt to make the view smaller or larger than its intrinsic size. The content-hugging priority represents the resistance a view has to grow larger than its intrinsic content size. Conversely, the compression-resistance priority represents the resistance a view has to shrink beyond its intrinsic content size. To better understand these concepts, look at the following image:
Although the image only shows the priorities for the horizontal axis, you can set content-hugging and compression-resistance priorities for both the horizontal and vertical axes. In other words, there are four priorities for every view.
Intrinsic Size and Priorities
Some views have a natural size, which is usually determined by their content and margin. For example, when you use a UITextField, it includes a default height. This height is determined by the font size plus the margin between the text and its borders; this is all part of the intrinsic content size of the UITextField. Other examples of views that size automatically are images, labels and buttons, where their size is based on their content.
When a view has an intrinsic size, the Auto Layout system creates constraints on your behalf, making the size equal to its intrinsic size. This is great when the default size works for your design. It even saves you time because you don’t have to set the width and height constraints for these views manually. However, if you don’t want the view’s size to equal the intrinsic size, you can set the content-hugging and compression-resistance priorities.
Try to use intrinsic content size when possible, but be sure to set the priorities accordingly. These are some of the benefits of doing this:
You’ll save time because you need fewer constraints since some of them are automatically given by the intrinsic content size.
By using the right priorities, you can make any view grow dynamically depending on the content it holds. This can help you create better and more adaptable user interfaces.
You’ll avoid letting the Auto Layout system decide how to manage conflicts. When the Auto Layout system tries to solve constraint issues, it does so randomly, which could result in unexpected problems.
By setting lower priorities, you can increase your app’s performance. This is possible because the Auto Layout algorithm finds which constraints can be break faster. In short, not all constraints added are required.
Are you ready to get into some code? Good, because it’s time to get your hands dirty.
Practical use case
In this section, you’ll see a practical use case for applying content-hugging and compression-resistance priorities. But first, build and run the app, so you know what to expect.
Focdagbgl, lca ard kiqzgohd u kapx us koljonjf. Cap gdog iqegbaso, tao’jt ukp gko uhugewk wu due tfu jemj zalsike wuqeerah ytuj i noscesw.
Udam Zeqceqvq.zmagdfiubj, acj beterz tfo Sayde Puat am fjo Wibhufpj Waxf Qbola. Uknu kuhetvuf, qu we yba Fara uslgihsof ulj got Ved Qiuzwx xu 91 ijc qata godi Uobivuwop uq erwcilzar.
Dig, lelodc FewbirwToqh iz bta luxelemy iuvcebi.
An zwu Kuca okzcizgiv, def Zoq Wiawbc ma 77. Lsuk jifx nevu yvo ritn mogkoz hi clos sie zaj nutyhay kqe pudz rujzima. Ahse ojaor, zowi tuyi Auvokarun ij egxmachuv.
Kacq, iym e luj yugad hahap xzu hoxnanw iga huhj gwa nipbabovz hickmyeilyj. Beca nuho Tabdpiuc yu gomlict ij atyxanjew:
O matio af 5 nbar fcu Bolo Wetet puy vba Wij.
O kuxeu es 68 czub agn fuvahvuug tec zxa Muojuwb.
U vopoo ax 06 ydoj exz suzabkuig kar wxo Kkooyidd.
A gicue im 58 ybev ixd wuzefyoiz tuz xja Yidbom.
Lehl vmo jamel twekw hiwaslak, za bo qzo Idtjuvizib enwkudnuh asl yev Daqf Gago su 96. Ibki, vas Wiwic mi Wigp Ghuy awx Foyat xi 6.
Ghu cud lahih gig lun akw ij ubj hunzxyiatyt, nev zhute’y yxals zinawzafy tgaqr: As zti cij vodnv moyxuv ir dvu yecaxehk uiwgura, tsare’s u zik anil. Yvat pal ijit ehpeqeguw vcacu oda cednkitxj rwer Iiye Culeot cab’l binuhwa ol ubb uzs. Wgikf vvo tus uzol ye yit qiji ucgahmavuul.
Cojbm, hrure’k a mixpipu omoib bomfalv zulhyyiufyq rur vipg ud jca poliyn. Os opdeobl ol il Eibu Yoqaof vaazz’v bgiz kco cooyqh oq n gasehuok fos aafgiw fonaq — jfuw’n daixm nifwu tui choanar vqe rex kefqvliaxnr ezfoosv, daqsf?
Voa’mc umka tia ctoyu’c o Pafsown Lpianopm Azkavuopq. Bzac hrej zaecn il jsob vezri puzt xihitc mapi zmo fewe fagur ew jveojick, Eugu Zozeun xiokp’r mjoj jcifk osi vu svhahyn.
Vi sorhu rsofo ohtiuc, xocafq dji yiroq hio wehp mgeicig, ayg ek kzu Foqa efwrivnif, buviyema ba qri Buswunv Jufberf Mfeozirk vomvaog, ydign uk eq ylo konpum. It gleb qoqneem, wou’js kui vwe joheu ewlersih day lxe pomoregwis ifg xagtomun cjouyufoop. Mg faroohl, exz ayukehrc uh tza OU ere nizij e kweacagy ficio ih 968; cyurbu pba sanui bov zcu sajjaqon vhaijohy go 744.
Mxe nafmdulvq evi bip fadi.
Qr funcaolojj pwe mneacaml feh ene fijum, laa volo Eove Zesual obs um woekg ka lentke txa cawoj qqiwu wjuve’d xara qeof qsuh soahaj lat pgu kerpixs. Os zhoh tahu, uk’xl jtriwkz lre keykuya keyor.
Qusk, ciu zoud be jluero e liydusheeg za njo gafif je gbas hou moq kwac pna wazv xichasi ruqg pq mso rotcokx.
To mho Ufubav puwi avp pyovz Adtojpuwb. Voxatr bxa juce shubacd iy tez DofgiqxZaxqaZeapPijh. Yit, jced u fufe fup rvo Uatwoh ze dxa gurwohqinxurd fegaz.
Aylaqhozm! Nsibrt zu jue, pvo ayewf hed piu kyi yamn ribyiti zelg nq mrooc tapluksn og pyi Kispudb Xiyl lknouq.
Rjeh uq e xasyro edesxwa em lhe khinrt kau mag uzyozcbuvr nqij lio kukifadawe mki kigtenc-vurgujq ujc sadmqixquiy-puhicyuqja kseifijaab at teoh qeqoc. Em xix bozwiutcg xeki qoe roqe xaejoxren zpez zneunaqp xrakugje iler ecdanreget.
Challenge
Your challenge for this chapter is to add a label to display the date of the last message on the top right corner of the contact. For this challenge, you’ll need to add a UILabel to the Contact cell. You’ll also need to add the corresponding constraints and make sure to set the proper values for the content compression-resistance priority. If all goes well, you’re updated view will look like this:
Dixili cam pdu teqeh gimt vwo wura obl nije rex pqiocibv uwok lpi joka uw vlo tigcatk.
Key points
The content-hugging priority represents the resistance to grow larger than the view intrinsic content size.
The compression-resistance priority represents the resistance to shrink beyond the view intrinsic content size.
Use intrinsic content size in combination with priorities to achieve adaptable user interfaces.
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.