简体   繁体   中英

Want to implement UIPageController and UIScrollView in UIView programmatically in ios

I have one view Controller and one UIView now in my ViewController when i click barButton than second view will apear like popup,now i want to add page controller and scrollview in that second view, my second view is not UIViewController but it is UIView.so how can i do that...

my first view is "ImageViewController" and second view is "PopupView"

in ImageViewController.m

#import "PopupView.h"

@implementation ImageViewController

- (void)viewDidLoad
{
UIBarButtonItem *clipArt = [[UIBarButtonItem alloc] initWithTitle:@"Clip Art"
                                                                style:UIBarButtonItemStyleBordered  
                                                               target:self
                                                               action:@selector(popUpView:)];
}

- (void)popUpView:(id)sender {
    CGRect * imageFrame = CGRectMake(10, 90, 300, 300);
    PopupView *popUpView = [[PopupView alloc] initWithFrame:imageFrame];
    [self.view addSubview:popUpView];

}

And in PopupView.m

- (id)initWithFrame:(CGRect)frame
{


    if (self) {
        CGRect * imageFrame = CGRectMake(0, 0, 300, 300);
        self = [super initWithFrame:frame];
        UIImageView *starImgView = [[UIImageView alloc] initWithFrame:imageFrame]; //create ImageView 

        starImgView.alpha =0.8;
        starImgView.layer.cornerRadius = 15;
        starImgView.layer.masksToBounds = YES;
        starImgView.image = [UIImage imageNamed:@"black"];

        [self addSubview:starImgView];
        self.backgroundColor = [UIColor clearColor];
}
    return self;
}

Implement like this in the second view, for scroll view with page control (this example illustrates the scenario for two views):

CGRect scrollViewFrame = CGRectMake(ur dimensions for scroll view);
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
myScrollView.pagingEnabled = YES;
myScrollView.contentSize = CGSizeMake(scrollViewFrame.size.width * 2, scrollViewFrame.size.height);
//content size is the actual size of the content to be displayed in the scroll view. 
//Since, here we want to add two views so multiplied the width by two.

CGRect viewFrame = [myScrollView bounds];

UIView *view1 = [[UIView alloc] initWithFrame:viewFrame];

viewFrame.origin.x = viewFrame.size.width; //setting the offset so that view2 is next to view 1
UIView *view2 = [[UIView alloc] initWithFrame:viewFrame];

//add both the view to scroll view
[myScrollView addSubview:view1];
[myScrollView addSubview:view2];

//add scroll view to parent view
[self addSubview:myScrollView];

You can replace the view1/2 with any visual element. To have scrolling with page control make sure all the view that you want to add to scrollview are so same width/height. That way it work out of the box and you wont have to do anything. Also, its always a good idea to add UIPageControl to the view for giving user some kind of feedback. Its optional though.

One more thing, if you want horizontal scrolling with page control, increase the width as done above. If you want vertical scrolling, increase the height and keep the width same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM