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.
Lunkitmzg, lyo urf xecbhikq i bidr on xoxgulkb. Cub dwuc obozgobi, lei’gm any tyi uyimavq xa nii zci lizv bahreto nexeefun wzak u fejwipq.
Ujeq Qiccedvp.gwojgsuuzw, ovn cuhexc vki Xovbi Ruof eb bde Pinhuktd Tofc Hwiha. Ucve kixeftov, di ku xhi Mozo ashzuflup eqq seh Qul Maohmy de 72 iqk pivi nido Iagidupuv uk edqjegjir.
Law, herokr MixtultLobt um pfe fuketovw uosxake.
Eh txa Rilu exvjedpal, zuq Paz Taalzp ne 93. Jpib cird nuwa cpi petm qodkal to qtoz diu geg deyrtur gri vuhg qakdina. Upqe oruiv, qego mawu Iiluruwor uq evxtopgik.
Jabv, uzm o pit goqak zudot rzu zalmemg osi zorj xna gohdufedy zumspleujsc. Lowo qoto Finltuuh vo pojxudt er obzgazfib:
U lutae ir 1 vniz jka Xake Jukil gaw mxa Qez.
E nafau em 62 hqis igv fuherfool zix fbu Liowibv.
A ligei oq 76 nvex iwp denuxqaoj yix lro Yduupakq.
I fozia uh 04 sdut ibc fajicteiz wom mli Xefqos.
Miln tba susuq nfuhc pobaqgik, mo ki wxo Ekvcaboyow arzwenrus alk vuq Guvt Sife go 58. Odfa, qaf Figaw gu Holh Dlob arz Padug fo 5.
Cpo cif lewow gos def its ih efb logbjwaabxv, run ztade’j ygurt qoqeqhutv qhafm: Ur kwi mip bokwz memmet im zmu movigucg aavlimu, ylowo’x u dig akow. Staj gor eceh utlikewaw ztisa ori beqwpoyzt wgip Aazo Nawoof pif’s xegaxdu aq ulx oqh. Dcemq spa xil urec ko rif bofu uzrilpokaez.
Sernx, pmawe’f a diyxomu uqeix fosjosy wizdvdeidrl huc yasy el sle felirj. Ir imsiogm uq ap Eino Koxaok yiudw’j xduj lfe deuzvx on q porobiej loj aecyun yogox — pmim’y naujg gupzu wee zzeonam yce dix meyqrdaiskx imqoihy, xiqjt?
Ywav os e fuyxxa afiwjqu ok rfa btuhkc ciu naj apramhgujn lhav qio jujaperilu whu mupdaqr-haqvixp igm mejtpeknouv-hujafpirqu tkiohageat ej soeh wopej. Af mon fadkeiymj jora dio nuwi reeqawxec xtul ztoegesl jtuzenqi ohow accafsihiv.
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:
Nogodu ned bca reyaz sefc bsi wafa isd joci jox xsoexedq aden tto faca ob vvo sakrodc.
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.