0

我的 ViewController 现在有一个视图控制器和一个 UIView,当我单击 barButton 时,第二个视图会像弹出窗口一样出现,现在我想在第二个视图中添加页面控制器和滚动视图,我的第二个视图不是 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;
}
4

1 回答 1

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 替换为任何视觉元素。要使用页面控件进行滚动,请确保要添加到滚动视图的所有视图都具有相同的宽度/高度。这样它就可以开箱即用,您不必做任何事情。此外,将 UIPageControl 添加到视图以向用户提供某种反馈总是一个好主意。虽然它是可选的。

还有一件事,如果您想使用页面控件进行水平滚动,请按上述方法增加宽度。如果要垂直滚动,请增加高度并保持宽度不变。

于 2012-12-04T13:49:36.460 回答