ios - UIView Autoresizing -
i spend hours (if not days) trying figure out (i'm several hours it!!!)
i have uiview contains subviews (see attached graphic) , built before added subview anything.
at point in time, add subview view uitableviewcell. call
[tableviewcell.contentview addsubview:mysubview] //.... [mysubview setbounds:tableviewcell.contentview.bounds]
and when init subview have:
[self setautoresizessubviews:yes]
from graphic, can see assuming width changes. have right hand subview of mysubview expand full width while maintaining it's original origin?
i approaching wrong in thinking , welcome suggestions!
edit assistance, khanh nguyen
so, stated, when init v1, (and, definition v2) don't know dimensions of v1.
when know bounds want place v1 in assuming can this:
[v1 setbounds:(some cgrect)]; v2.autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexiblebottommargin;
or c set autoresizingmask on v2 instantiate (when still don't know dimensions of v1)?
if have view v1
, subview v2
, want v2
have fixed left edge, while width changes when v1
resized (i.e. distance between v1
's right edge , v2
's right edge constant), use this:
v2.autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexiblebottommargin;
for swift:
v2.autoresizingmask = [uiviewautoresizing.flexiblewidth, uiviewautoresizing.flexiblebottommargin]
by default, autoresizessubviews
yes. don't need set it.
edit
every view has dimensions, if don't know (just nslog
if want know it). if view has been init'ed, dimensions (0, 0). autoresizessubviews
works even in case, long know how spacing need between subview , superview.
for example, want v2
10px v1
left margin , 20px right margin, following do
// v1 has been initialized, , dimensions unknown (to you) // it's (0, 0), doesn't matter // 30px = 10px + 20px v2.frame = cgrectmake(10, 0, v1.bounds.size.width - 30, 100); v2.autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexiblebottommargin;
note third line, v2.frame
set negative numbers if v1
has 0 size initially, latter when v1
's frame set proper size (either or uitableviewcell
), v2
resize accordingly (because of autoresizing constraint) , become want achieve.
Comments
Post a Comment