ios - What is a correct way to report desired size for UIView subclass? -
i subclassing uiview , cannot find right way request/report desired size. expected systemlayoutsizefittingsize
trick, never called. layout simple - pin top , leading of view , limit trailing , bottom less or equal top-level view. among sizing functions, intrinsiccontentsize
called, 1 not helpful.
import foundation import uikit class statsview: uiview { override func drawrect(rect: cgrect) { let context=uigraphicsgetcurrentcontext()! cgcontextsetrgbfillcolor(context, 0, 0, 1, 1) cgcontextfillrect(context, rect) } override func sizethatfits(size: cgsize) -> cgsize { print("called sizethatfits \(size)") if(size.width>350) { return cgsize(width: 350,height: 50) } else { return cgsize(width: 50,height: 100) } } override func systemlayoutsizefittingsize(size: cgsize) -> cgsize { print("called systemlayoutsizefittingsize \(size)") return super.systemlayoutsizefittingsize(size) } override func systemlayoutsizefittingsize(size: cgsize, withhorizontalfittingpriority horizontalfittingpriority: uilayoutpriority, verticalfittingpriority: uilayoutpriority) -> cgsize { print("called systemlayoutsizefittingsize \(size)") if(size.width>350) { return cgsize(width: 350,height: 50) } else { return cgsize(width: 50,height: 100) } } override func intrinsiccontentsize() -> cgsize { super.intrinsiccontentsize() print("called intrinsiccontentsize") return cgsize(width: 10,height: 50) } }
what right way it?
update want view have info, not blue rectangle. there "optimal" width, if view cannot have parent, can compensate rearranging information use more height. so, reporting constant values intrinsiccontentsize
not fit needs.
you use intrinsiccontentsize
report desired size of view based off of it's contents. value can change contents change, need call invalidateintrinsiccontentsize
when does.
this intrinsiccontentsize
works along contenthuggingpriority
, contentcompressionresistancepriority
, , other layout constraints in superview determine actual layout size.
in case sounds want report ideal size based off of contents of view, change size , invalidate if content changes, , use layout constraints sibling views , parent view compress (or resist) needed.
Comments
Post a Comment