How should child views of UIScrollView report their bounds for contentSize?
I'm looking more for advice on the correct design for a view.
What I have is a UIScrollView that contains one or more custom Views I have created. My problem is, who reports to the scrollview what it's contentSize should be? I have the following:
UIView
+-UIScrollView
+-CustomView 1 with dynamic height depending on data
+-CustomView 2 with dynamic Height depending on data
The UIViewController creates new instances of the custom views with data and then adds them as subviews to the UIScroll开发者_高级运维View. The problem I'm having is how to set the value of the scrollview's contentSize? Right now, I'm not doing that and the contents of the scrollview are clipped with no scrolling possible.
- Should the custom view call [parent setContentSize:] in its drawRect:?
- Should the UIViewController query the custom view after creation to get its bounds and then call setContentSize?
- Should I subclass the UIScrollView to override addSubView to query each subview's height?
- Is there something else I'm missing?
I hope I explained that properly. I'm new to this and still getting a handle on things.
The contentSize of the scroll view should be the size of the union of the frames of all your custom views. Whenever the size of a custom view changes, or one is added or removed, the view controller should calculate the new contentSize and apply it.
Setting it from drawRect: could essentially set up an infinite loop.
Using the bounds does not give the coordinates within the parent view.
You could subclass UIScrollView if the custom views do not change size.
精彩评论