Tuesday 15 July 2014

ios - Custom UIView drawing with layoutSubviews vs drawRect -



ios - Custom UIView drawing with layoutSubviews vs drawRect -

should drawing of cashapelayers called drawrect() or layoutsubviews()?

according apple's documentation on uiviews, drawing operations should placed in drawrect().

however not case in kevin cathey's wwdc session: what's new in interface builder (where demonstrates process of building custom uiview accessible in interface builder).

during demonstration performs drawing of view in layoutsubviews, rather drawrect.

his explanation that:

if implement drawrect, that's not going best performance; whereas, using sublayers , subviews going performance.

from i've read far on stackoverflow, seems overriding drawrect() method can cause reduced performance. partially due setneedsdisplay triggering manual redraws beingness expensive.

but looking through apple documentation , real world applications. makes sense drawrect() should responsible drawing of view, , layoutsubviews() handling positioning.

i depends. if size of shape going dependent on auto layout scheme might beneficial drawing in layout subview versus draw rect.

first, why layout subviews ever have improve performance? have uiview (my parent view) drawing circle view (my subview) using shape layer. size of circle determined width , height constraint in parent view. ever need redraw circle when either width/height constraints changed on parent view. instance beneficial have drawing code in layout subviews because ever need redraw shape layer when constraints change. let's see auto layout cycle

update constraints layout subviews render views

by time layout subviews called know size of frame working , can safely perform our drawing. know documentation says should utilize method set frame, bounds, etc. however, drawing within frame doesn't sound unreasonable depending on circumstances.

if circle view isn't dependent upon auto layout scheme , changed @ anytime regardless of layout of ui, more beneficial have drawing in draw rect can updated. think less ideal situation more ui's in ios conform using auto layout, hence recommendation of using layout subviews.

ios uiview

No comments:

Post a Comment