想在ios中以编程方式在UIView中实现UIPageController和UIScrollView

时间:2012-12-04 12:31:52

标签: iphone objective-c ios cocoa-touch

我有一个视图控制器和一个UIView现在在我的ViewController中,当我点击barButton比第二个视图将像弹出一样,现在我想在第二个视图中添加页面控制器和scrollview,我的第二个视图不是UIViewController但它是UIView.so我怎么能这样做......

我的第一个视图是“ImageViewController”,第二个视图是“PopupView”

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];

}

在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;
}

1 个答案:

答案 0 :(得分:0)

在第二个视图中实现这样,对于带页面控件的滚动视图(此示例说明了两个视图的场景):

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];

您可以使用任何可视元素替换view1 / 2。要使用页面控件进行滚动,请确保要添加到scrollview的所有视图都具有相同的宽度/高度。这样它开箱即用,你不必做任何事情。此外,最好将UIPageControl添加到视图中,以便为用户提供某种反馈。它是可选的。

还有一件事,如果您想要使用页面控制进行水平滚动,请按上面的步骤增加宽度。如果要垂直滚动,请增加高度并保持宽度相同。