0

I have gone through many tutorials but most of them tell how to add a UIScrollView using the interface builder. I need to add a scrollview using code. I went through several tutorials and what they did was they add the UIView to the UIScrollView . [scrollView addSubView:myView]; simple as that ad connect to using interface builder.

Can someone please guide me as in how to add a UIScrollView using code (Only Code)

2 Answers 2

1

Is really simple:

UIScrollView * scroller = [[[UIScrollView alloc] initWithFrame:CGTectMake(0, 0, 320, 480)] autorelease]; [self.view addSubview:scroller]; int x = 0; for (int i = 0; i < 5; i++){ UIView * myview = [[[UIView alloc] initWithFrame:CGRectMake(x, 0, 320, 480)] autorelease]; [scroller addSubview:myView]; x += myView.frame.size.width; } [scroller setContentSize:CGSizeMake(x, 480)]; 

This code was compiled in my head, so sorry if there is a sintax mistake.

Sign up to request clarification or add additional context in comments.

2 Comments

I guess self.view is the 1st half of the scrollview and myView is the second 1/2. SO if i want to add some labels etc on the bottom half of the scrollView i need to add the labels to myView ?
´self.view´ is the main view of your controller, and ´myView´ is any view you want, it could be UILabel, UIButton, UIImageView, anything, and in the ´frame´ you specify any size or position you want!
1

All my examples use code rather than the nib - it's way easier to create and configure a scroll view in code! See here:

https://github.com/mattneub/Programming-iOS-4-Book-Examples/tree/master/convertedToIOS5

The scroll view examples start with p482p494scrollViewInCode.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.