I am trying to add UIViews above its origin.
For example
Suppose i have added subview A at origin (x= 0 , y = 0);
now i want to add another subview B above this subview so i tried
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -500, 200, 500)];
view.backgroundColor = [UIColor redColor];
[scrollView addSubview:view];
[scrollView setContentOffset:CGPointMake(0, 500)];
[scrollView setContentSize:CGSizeMake(300, scrollView.frame.size.height+500)];
I want to add many views like this while scrolling to top i.e. when scrollview reaches at top add another view above current view and scrolling should be without any lacking.
But with above code it does now show above added views.
If I'm understanding this correctly, you're looking at this the wrong way. If you have a scroll view with a total content size of lets say 1000, and you set the content offset to 500, then if you want to add a view that is 500 above the visible top of the scroll view, you should set that views origin to 0. Although you want this view to appear 500 pts above the currently visible portion of the scroll view, you have to create the coordinates in the coordinate space of the scroll view, which start at 0,0 left to right, top to bottom. Try this instead:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 500)];
EDIT:
Since you want to move this view above an existing subview, you have to make adjustments to the frames of both objects. The new view and still be set up as listed above, but you would need to call setFrame:
on the existing view, and set its y origin to a value measuring at least the height of the view that you've added above it.
You should calculate the total content size up-front (as UITableView
does by calling heightForRowAtIndexPath
) and place your subviews within the scroll view's bounds. So in this case, view A would be at (0, 500) and view B would be at (0, 0). You can add and remove views dynamically as scrolling happens, but setting the content size up-front is going to make things a lot easier.