append UIView's to the top of a UIScrollView

For example in Tweetbot's iPhone app. When you open up the app and new tweets come in, it will just be appended to the top of the UIScrollView and the current tweet you see did not get refreshed. How can I achieve the same thing effect?. Say I have a UIScrollView with a UIView added to the UIScrollView starting at origin 10,10.

I then downloaded a few items and I want to put it at 10,10.. so I basically need to shift this old item at 10,10 down right? If I do so then during that shifting user's will see it shifted, which is not the effect I want. Where as in Tweetbot's app it seems that nothing is being shifted around, it's just that you grow the area above the 10,10 and append new stuff's there.

How do I do this?

Basically I wanted to implement the insertRowAtIndexPath in a UIScrollView.


Will restate the question this way: how to add content to the top of a UIScrollView without moving the content that's already there (relative to it's current offset).

If this is the right question, the right answer is to do the add and shift down just as you suggested, but then scroll by the same height as added content, giving the illusion that the old content didn't move.

- (void)insertRowAtTop {

    // not showing insert atIndexPath because I don't know how you have your subviews indexed
    // inserting at the top, we can just shift everything down.  you can extend this idea to
    // the middle but it requires that you can translate from rows to y-offsets to views

    // shift everything down
    for (UIView *view in self.scrollView.subviews) {
        if ([view isKindOfClass:[MyScrollViewRow self]]) {
            MyScrollViewRow *row = (MyScrollViewRow *)view;          // all this jazz so we don't pick up the scroll thumbs
            row.frame = CGRectOffset(row.frame, 0.0, kROW_HEIGHT);   // this is a lot easier if row height is constant
    MyScrollViewRow *newRow = [[MyScrollViewRow alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,kROW_HEIGHT)];
    // init newRow
    [self.scrollView addSubview:newRow];

    // now for your question.  scroll everything so the user perceives that the existing rows remained still
    CGPoint currentOffset = self.scrollView.contentOffset
    self.scrollView.contentOffset = CGPointMake(currentOffset.x, currentOffset.y + kROW_HEIGHT);

If you set the contentOffset without animating there wont be a visible scrolling animation. So if your new view is 200 points tall you can set the origin of the new view at (10,10) and the old view at (10,210) and set the contentOffset of the scroll view to (10,210) you should achieve the effect you intend. You'll also need to increase the contentSize of your scroll view to be big enough for all of the content it contains.

Need Your Help

Single page web without absolute position

css position singlepage

is there any way to make single page website without position absolute? Because when I want to variable height of containers, absolute position is little bit awkward. I mean when I insert more cont...