Adina Marin
Adina Marin

Reputation: 663

Fix next image in scroll view like in page view

I created a scroll view , but i want to made it to act like a page view .

- (void)setupScrollView:(UIScrollView*)scrMain {
            for (int i=1; i<=11; i++) {
                image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
                      if (i==1) {
                    imgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, 2, 289, 470)];
                }
                 else
                    imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*350, 2, 289, 470)];
                imgV.contentMode=UIViewContentModeScaleToFill;
                [imgV setImage:image];
                imgV.tag=i+1;
                [imgV bringSubviewToFront:self.aleatoire];
                [scrMain addSubview:imgV];
                UIImageView *aleatoire=[[UIImageView alloc]initWithFrame:CGRectMake (self.aleatoire.frame.origin.x-19,self.aleatoire.frame.origin.y,self.aleatoire.frame.size.width,self.aleatoire.frame.size.height)];
                aleatoire.image=self.aleatoire.image;
                [imgV addSubview:aleatoire];
                [imgV bringSubviewToFront:aleatoire];
                UIButton *rand = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [rand addTarget:self
                         action:@selector(randomBtn:)
                  forControlEvents:UIControlEventTouchUpInside];

                rand.frame=aleatoire.frame;

                rand.backgroundColor=[UIColor clearColor];
                rand.userInteractionEnabled = YES;


                imgV.userInteractionEnabled=YES;
                [imgV addSubview:rand];
                [imgV bringSubviewToFront:rand];




            }
            [scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*11, scrMain.frame.size.height)];

        }

but i have this issue:enter image description here

the next image is not in the center . Any ideea where i'am wrong ?

Upvotes: 0

Views: 303

Answers (1)

Dharmesh Dhorajiya
Dharmesh Dhorajiya

Reputation: 3984

You set UIScrollView paging pagingEnabled property enable to create scroll behavior like pageView.

try this code :

scrMain.pagingEnabled=YES;
scrMain.contentOffset = CGPointMake([[UIScreen mainScreen] bounds].size.width, 0); // set your size to scroll content

Simple way to create pagerView in UIScrollView

int x = 0;

    for (int i=1; i<=11; i++) {
        UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height)];
        img.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];

        [self.scrollView addSubview:img];
        x = x + [[UIScreen mainScreen] bounds].size.width;
    }
    self.scrollView.contentSize = CGSizeMake(x, [[UIScreen mainScreen] bounds].size.height-64);
    self.scrollView.pagingEnabled = YES;

    self.scrollView.contentOffset = CGPointMake([[UIScreen mainScreen] bounds].size.width, 0);

Upvotes: 1

Related Questions