Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tutiplain

macrumors member
Original poster
Feb 4, 2011
95
0
Hi all,

I've been experimenting with scroll views lately. According to the docs, for it to work we need to specify the scroll view size in our program code. The examples I could find handle it on the viewDidLoad method, like this:

Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIScrollView* scroll = (UIScrollView*)self.view;
    scroll.contentSize = CGSizeMake(320,900);
}

I understand why we need to cast self.view as a UIScrollView (because self.view has no contentSize property on its own). But I found this approach to be a bit complicated and tried to simplify it. Instead of casting self.view to an UIScrollView*, why not just have a property set up and pointing to the UIScrollView? So the viewDidLoad method looks like this


Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.scroll.contentSize = CGSizeMake(320,900);
}

Where self.scroll is declared in the view controller's header as:

Code:
@property(nonatomic,retain) UIScrollView* scroll;

And this property is connected to the UIScrollView on my nib file, which, in turn, is connected to the view controller's view outlet.

However, it didn't work. The contentSize property was not set, and I could not scroll my view. My question is, why wouldn't this work? Wouldn't self.view and the scroll property be pointing to the same object?

Thanks for any info on this!
 

cthesky

macrumors member
Aug 21, 2011
91
0
Hi all,

I've been experimenting with scroll views lately. According to the docs, for it to work we need to specify the scroll view size in our program code. The examples I could find handle it on the viewDidLoad method, like this:

Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIScrollView* scroll = (UIScrollView*)self.view;
    scroll.contentSize = CGSizeMake(320,900);
}

I understand why we need to cast self.view as a UIScrollView (because self.view has no contentSize property on its own). But I found this approach to be a bit complicated and tried to simplify it. Instead of casting self.view to an UIScrollView*, why not just have a property set up and pointing to the UIScrollView? So the viewDidLoad method looks like this


Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.scroll.contentSize = CGSizeMake(320,900);
}

Where self.scroll is declared in the view controller's header as:

Code:
@property(nonatomic,retain) UIScrollView* scroll;

And this property is connected to the UIScrollView on my nib file, which, in turn, is connected to the view controller's view outlet.

However, it didn't work. The contentSize property was not set, and I could not scroll my view. My question is, why wouldn't this work? Wouldn't self.view and the scroll property be pointing to the same object?

Thanks for any info on this!

Hi Tutiplain,

It should can be done. I have listed the steps to do this:

1.) Create a new project named ScrollView. (Lets say a new View Application).

2.) In ScrollViewController.h, write the following code:
Code:
@property (retain, nonatomic) [COLOR="SeaGreen"]IBOutlet[/COLOR] UIScrollView *scrollView;

3.) In ScrollViewController.m,

- Synthesize the scrollView:
Code:
@synthesize scrollView;

- For viewDidLoad method, write the following code:

Code:
- (void)viewDidLoad
{  
    [super viewDidLoad];
    [COLOR="SeaGreen"]self.scrollView.contentSize = CGSizeMake(320, 900);[/COLOR]
}

4.) In ScrollViewViewController.xib, drag a Scroll View from object library to view. Then, connect the scrollView outlet to this newly added Scroll View.

5.) You can run your project, it should be ok. :)


One more thing is for the code you pasted,

Code:
@property(nonatomic,retain) UIScrollView* scroll;

It should be

Code:
@property(nonatomic,retain) [COLOR="SeaGreen"]IBOutlet[/COLOR] UIScrollView* scroll;

Thanks. :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.