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


当我单击 barButton 时,我的视图控制器中现在有一个视图控制器和一个 UIView,第二个视图

会像弹出窗口一样出现,现在我想在第二个视图中添加页面控制器和滚动视图,我的第二个视图不是 UIView控制器,但它 UIView.so 我该怎么做......

我的第一个视图是"ImageViewController",第二个视图是"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];
}

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

在第二个视图中实现如下方式,用于具有页面控件的滚动视图(此示例说明了两个视图的方案):

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 添加到视图中以向用户提供某种反馈始终是一个好主意。不过它是可选的。

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

相关内容

  • 没有找到相关文章

最新更新