gdm
gdm

Reputation: 7930

Add 2 UIViews to UIScrollView

Is it possible to use UIScrollViewController to scroll or "page" two or more views coming from UIViewControllers? Example (in viewDidLoad)

        self.a1    = [[CustomViewController1 alloc] init];

        self.a2  = [[CustomViewController2 alloc] init];

        //Scroller
        self.scroller   = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,1000,400
                                                                        400)];
        [self.scroller addSubview:self.a1.view];
        [self.scroller addSubview:self.a2.view];

        [self.view addSubview:self.scroller];

But I can see in the scroller only the view of controller a2.

Upvotes: 1

Views: 198

Answers (4)

Guy Kogus
Guy Kogus

Reputation: 7341

You'll need to add the custom view controllers as child view controllers. See Apple's documentation for more information on doing that correctly.

Upvotes: 0

gdm
gdm

Reputation: 7930

The solution is to set the frame coordinates of the subviews inside the scroller:

CGRect frame = CGRectMake(0, 0, 1000, 600);
self.a1    = [[CustomViewController1 alloc] init];
self.a1.view.frame = frame;

self.a2  = [[CustomViewController2 alloc] init];
frame.origin.x = frame.size.width +10;
self.a2.view.frame = frame;
[self.scroller addSubview:self.a1.view];
[self.scroller addSubview:self.a2.view];

[self.view addSubview:self.scroller];

Upvotes: 0

romeouald
romeouald

Reputation: 186

You have to modify the frames aswell. Now a1 and a2 have the same, and they are on top of eachother, so the a2 is visible, because you added the later.

Try

self.a2.view.frame = CGRectOffset(self.a2.view.frame, self.a1.view.frame.size.width, 0);

Upvotes: 1

Ben Clayton
Ben Clayton

Reputation: 82219

You need to set the frames of the views so they they sit one below another. Currently they are both there, just on top of one another.

Try putting in

self.a2.view.frame = CGRectMake(0, 480, 320, 480);

Upvotes: 1

Related Questions