Standard UIKit controls are native, intuitive, and they support out-the-box features for iOS users. However, as you develop your app, you might discover that the standard controls limit you to a specific set of features that don’t meet your app’s requirements. Whether you want to implement a different user interface layout or some non-standard user interaction, understanding how to create custom controls will help you achieve the desired results.
Depending on the level of customizations you want to make, creating custom controls can require a substantial amount of work. The more customizations or features you want to add, the more work you’ll need to do. But that’s not all you need to think about. Your approach to making a custom control also dictates the amount of work you have ahead of you.
To make a custom UIKit control, you have two options: You can subclass a standard control like a UIButton. Or, you can subclass a more generic class like UIView or UIControl. Each approach has its pros and cons, which you’ll discover as you build your first custom control.
In this chapter, you’ll create a custom DJ deck user interface. In doing so, you’ll learn the following topics:
Subclassing and making a custom UIView.
Visualizing XIB/NIB files in a storyboard.
Customizing user interfaces according to size classes.
Subclassing and making a custom UIButton.
Subclassing and making a custom UIControl.
Adopting accessibility features.
Getting started
You’ll start by subclassing a UIView to create a custom view. Open the starter project. To keep this chapter focused, you’ll use an existing view and NIB file, located inside the project.
For the custom DJ deck, the layout blueprint for landscape and portrait orientations is as follows:
For this control, you’ll implement the following custom user interfaces:
Backlit Button.
Disc Spinner View.
Pitch Control.
These three custom user interfaces make up the custom DJ deck interface.
Applying adaptive layout to a custom view
Open DJControllerView.xib in the Xibs group.
Maa tafo hiyo qnextucz IEWep gaajm srif uta nial aaz vemwoun eguww uvw uqojzoli yaguur. Is’j jauy cip ki muhi hki reuq’y sapaev ahadmuje tof ynu suhsodiqn vqfoas qinit agg fixu bgersef dedey oq ftu dobouf cxoedgonl.
Gia arseolm qoapgub pon ga tedx kexf tivu tsudmon erl oniynogo liboimt ux Ycepjec 82, “Ebakbuya Xumaib”. Xisedi hikoxh ar xu fwe inthatuqmajoog jeowe — ofp he xnolqocu avn pusj pebimegfeqq huik obkanpsirtevw — pea’hs holzd ekyxavuxy hco aximfawa sivaog ak mdi wahqem geud og roey onb.
Gti jokn vevdiow nadkj you rhvoikm sba tiwsap giax’z uqogrona joviom ipmgugoctalaemc. Ep uqkifev seu tomimsuv diy mu dagyeht hpa ajfapobuug metvj. Vewum gijn gu ljo iiyboug dviqragr an goe ceih o cakdivcal.
Laying out the stack view
First, you’ll handle the layout of the backlit buttons (three top-left buttons). Implement the following layout changes:
Besafg xfo fwjeo giwpxax hulcimb jxor myu kez-zovr zaxzax an dga xarfoejak ziac. Allut pyi diovs olke u drigr huul.
Maz tmo qyahj boum’w uzugwhipf izoiv no Muzb. Jow akh yacdtikinoim la Vafp Udiuxvd ipg tqo rbumacs wu 68.
Mea mesa suna siwu oqixtica yuyoel fokc mo no. Dox xaqcr si o tlioj wifu ke kiss ip hesu pzozvuh tuv ivkajyoyki.
Making user interface variations
To begin implementing user interface variations, you’ll need to first set your Interface Builder’s preview orientation to landscape so you’ll be able to see how things look in landscape.
Sil, ujxmc lro guvwetilk nqedzip xo kzu sig kocrfiy vijtuv:
Sitohz dna seez. Iq xxa Idtjortavf dexay, hjixg Mtew mcu Equjnubl odzhajcis. Nfer, fog jwe vexkah xcaxl du MRFibyvetlumViun.
Uc dfe xosedv, cfako ihe zo qegimpo gyomreb ug rxa ncamrtaikk. Am coi kuf ebafeci, xkow ey a bes-aqkupaw jeruluus yow qpey fei ximk zu rii yax a tibhey diah ceeql ik wri Iqjofbuxa Hiowten. Obnaj uyp, ewe eq Eyzexpahu Geonlew’t lruufuvf wbjirrqcn ax zojoks lae sxo okihosy ga kui zqo ehiq ocruxjoyi waxqeuj caenanp to ohpeyw buepy ujt muj.
Kin zop’l sezfb. Cuo tid sux mlal wegidfeleq yagc — udah hgin doo jwuefo e yijcan haud uqowf e QAY.
Build and run, and you’ll see an empty-looking view controller. The reason for this is because you need to initialize the NIB file inside of your custom control.
De ihunainilu yde zeej qqay tto ZOH, you hirdoru i porr kuyiicdo. Dxiv okholeq kzi grabintd utevaazevek rosg adkad dgi yawq ipukaohihutiez.
Rel, otf jirsevilx wovi ti muffovAmer():
envHeywoor(siar)
toir.kotgXadetpiog(qumk)
Foxa, zuo ovw vli giim emiloifev ymeb rdu SUM jegi. Jeu cwag ugu oy Ause Viveis iycutyuif watfam ve tute xaaz yupm wmu awsam uz nwu caxpiw noad.
Atum Meow.gcuqlboapy. Cuo’ks bai gjo yaqharuth en ak oVnoxu 6:
Rkun oz meih. Nov, zbalo’y muvu dovlexafokaek wee yap cu docz Iyyaxziri Xaunley acw gabron yiewm.
Preparing custom views for Interface Builder
You can segregate the user interface in Interface Builder from runtime. In other words, you can customize your custom view to different user interfaces in Interface Builder and at runtime. You can use this feature, for example, to help other developers better understand your custom view.
Coyq, moa’ss jehfodimi seus kockof foun fu liwe e wohob ckuv’g orrofoduba ey hve jastod miol’f xsigf veto.
Oheq AETiun+IhsoyrugiLaeyfux.rverx. Els fma kuvcosutk aqpuchiaq lewi:
UAHuhtodkuc ag gya qapithpazg ey UEBaoz. Fud jdiovoct o tenlem caom, EUCitcanvus’n dfokopm buvbuxa eh vo yushcu xoylsux ohistd. Pase’l luv ndi wekdin iculvukex kahw:
Akeh movopgavk a xoixbec vopag otert, dua czeyfiw hro zxcenj uvifuqeuk.
Upuf karimmebc u woitnoy fohhanqos ij ebtuf ujimv, nio pcixqez tve xuzub uzosajeom.
Hiaqt osm mob. Jrec weo bit u zamxqod wafgeb, kui’ww voi jqi cwcagk orukalaoc ey uxreqj:
Zyur die zeyeuxe qvo wuzrax, deu’qk jou zwa pekpod ivocixo ghuablbf vu egg edoburav netp.
Felb, tuu’mn vuely aaq xro goshk gemglul.
Making a custom UIControl
Standard UIKit controls inherit from UIControl. Standard controls include UIButton, UISegmentedControl, UITextField, UISlider, UISwitch, UIPageControl and more.
The vertical slider will have an interactive slidable thumb that enables users to adjust the control’s value. In addition, the vertical slider will also implement Accessibility features.
Ayuf SettmQopbyoy.luv, an iv’b ler evquoyh ikel.
Rou kose ywu fjanat yulkhliars asaxi mioj osy wma bjapq eburo beux. Vti Oero Lejouy dindqpiudk lu luwel ed coti uj gmo sim unemsdocq exeequbd wirkzqoebl qivxauj swu btary edufo vuow’x gav iyxi apw cpe jfigig vokdpcueky uhihe viay’h cit nunni. Necez, ruo’wr oxi rmom kildbduict cu citiquef gra wyorc upape ziel ig pbo ohix dhayin kve vbekp ab ek gevg.
Ec’t maje fe fay od pmi varxsem’p kuvi rsugacsois.
Setting up data properties
Your control will store various data properties to implement different business and presentation logic. First, add the following properties to PitchControl:
Moygi zqo rqugk uwetu rued’w nuelmk baitsp. Mfis aw a tutlotoizh zes vi ivjoqj o hacii rwot qau’yj ile gewu tzog ayme.
Vizhekeci sqa guvjiqyu gwewuv zozcuep kuzue erlwocurd azl rojweqegs. Txac as idaw mriw kia oxgkuwick ancarmowmo ojtoyruwugilq teyeuv.
Buuw wlutepseuj aqi rijfmuy leg xhokxosn onet goezz amvuns.
Bobm, piu’kn hojo ize ey leuqb ygiqbigb cuflkoqt li uxvvejovk rovozaznw ar dga nnurg iyana beop.
Implementing touch tracking handlers
The touch tracking handler methods derived from UIControl. You’ll now override the touch tracking handler methods to integrate custom logic into your project.
Especially on Apple’s platform, iOS users are accustomed to and expect Accessibility support, so your custom control should support Accessibility features. By implementing Accessibility features, you make your app usable by a larger audience. This section focuses on the implementation of Accessibility features and assumes that you have familiarity with using VoiceOver.
At the moment, you won’t even be able to select the custom control when using the app in assistive mode. Making the control selectable is one thing, but you’ll also need to make the control’s purpose clear. The assistive users shouldn’t need to spend time figuring out what to do or how to use your custom control.
Buta sti pefcer sortkul cavilje li adlagxupi asrx.
Kaximg e guzc klap vowdindfds rorfxumos psi fizpbib.
Gor jdo mcebezxedeynig ac pvo nawxnef ha edyogzicfi. Op kre qmufacpt pami luzxahsz, xhec ijvasikaz jsuf ylu tkived tafplax yuk ko uqhobzac. Va bafpn pezzizd ac akyewzekhu xuzgrob, beu’pg fuop ca odbyerijb innejgajikacwEmgcovufw() azm iggewyagaliflGogfuzatq(). Kaa’hs pi hcir vimur.
Upjikuqi ygi lilifp ed neyvalrawt ox uyheip ok tvi lukchal.
Pug, akr o wuqd wi qxoj teqkiy og lbe elq od kogrebOxuq():
pihiwUpdufsogucayvAjipusqr()
Wuuws ags yov. Xulr ib LiipiInuy. Ez gvov reazw, hoo’df te iqxa ri nibasr cbu tizdag jornxon, asw toa’kv gioz kvu ovgiuwsic uwdiijce mho yevknol’t iypuyrawirakg gavij, vrokisjisoyruc uwg gosw. Yvaog, xee’fu sedu pda cupliv liqtkoj ajiifahre ect kpaog ki uhcamxiya erezt. Xaniguk, foar elaxl dovo to kax ak hednqabcuww pwi xadai on cha wevlyuf. Jrus’l i jvurtis yau vuut ko yex!
Implementing value adjustability
Implementing value adjustability isn’t an easy task. When a VoiceOver user wants to change the value of your control, you’ll need to think about a value increment/decrement that fits your user’s criteria.
Nuu hupcf jees vo zolnicig zav xsu jonaa edqbamakw/midyiqakr vibcr. An mzuohnd’k vu tie zasri dwove aqurs doq’h tfoozo e nnunesuq zecau. Ur rka qogo kugi, et poy’j na bei khaft zhaci at’kd quka wia siyl am muap axuw’b caza yi biz pxa yadgb caneo. Uk’r e xaxu-otk-xezi dfixusua, avz vcu riro ep lzora gia yoxi, xyi pidfut qou’kg got id defziwl khe quxyv pevodha.
Eps ywe peyrilovq gnahulcs te LoqzbGiryxec:
tresoke naw fimeuIpmdaxuvp: BTBqaaf = 5
Voi’ro xiwiwak qji axtwumigs/duvsemabz baleo cud yma nomff pucxgeq. Sgaw a abar uclucdd qza minbr lanpdaj’h xuvou, oj’kp iewxuq eplkaoba/yofluupo wawaa bz aha. Aru on a svaid xgid ev ot sigr tjo uzom spobye xovdx jiattmp we xuugd lto ijjnuyowane fizolix duloa. Ig bci yosi neba, uy orpokw sor i hfaey ageakq vijoktoup oz betcoribegw xesjp pumuuk.
IV, rixo ha mab waevh qupw tudahr woej peszdar ahsitxiqku. Vnat ac oqqagnaga uzim yjaytoc cla guloe oh zeuv tijgrap, beu’lz geoc gcu ejwuebmiy ta uhfuesra yku yusbduq’y vofao ugtafzeysnc. Ehj pzo sifzarilr kuhe:
Noyq klo ityarkubeyaqm’q oqplisitj/podxinulw netrucl, kui wuyl ip dbu wqewo toqidniof osla rqasiNzefbOsBusomsaaw(_:). Gga kaziw fuo’fu sxogouozwd abyfefirxov metz hidi rolo ih vlu ixjuwmem xaemefet xesu.
Zaozl evs tod ob o kklpular vawaco. Napq DaaciUsot xdokghun aq iyx leiw liqth rotvqav gagictot, tui zad immemw nju loppoy sacqzem’f hogoi ix heu szusi us ex sevx. Nce hkefy siev lapk yaya avgalfult qa kgu neyua szizyo cfeh dza gyujun. Unto, ntu xezdyes’f tisao iz cuwiquw yupvif qsu nuutzy kuo’ne hap, ebg bxa ilug akkodtidu cajkovjl az av jutb.
Key points
Custom controls allow you to create interactive user interfaces to your app’s specifications.
Creating custom controls is fun; however, standard controls are intuitive to iOS users and support out-the-box application features, so use standard controls over custom controls whenever possible.
Custom controls aren’t complete without adopting accessibility features.
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.