Jonovono
Jonovono

Reputation: 2085

Add content dynamically to UIScrollView

So I think a UIScrollView is the best way to achieve what I want.

I will be adding 1 pixel width lines to the right side of a view every 1 second. I only want the last x number of lines shown on the screen, but then the ability to slide backwards and view previous ones.

For an example of what the view will look like:

Lines

From what I can tell I should use a UIScrollView to achieve this, but it seems that that wants you to define the width and number to render ahead of time, but I will be adding it dynamically while the user is using my app.

Any help would be great, thanks!

Upvotes: 0

Views: 312

Answers (2)

Catalina T.
Catalina T.

Reputation: 3494

I think using a scrollView will be pretty complicated. My suggestion is you could use a collectionView for that and use insertRowAtIndexPath: to insert new rows.

You will implement the delegate method for the cell size, looking something like this :

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(1, CGRectGetHeight(collectionView.frame));
}

Then you just need to set different colors for the cells background.

Let me know how it went or if you have questions.

Upvotes: 1

Ravikanth
Ravikanth

Reputation: 318

Check this Solution

CGFloat width = self.scrollView.bounds.size.width; CGFloat height = self.scrollView.bounds.size.height;//CGRectGetMaxY(textField.frame); self.scrollView.contentSize = CGSizeMake(width, height);

Upvotes: 0

Related Questions