如何将 viewController 放入 UIScrollView [英] How do I put viewController's into a UIScrollView

查看:22
本文介绍了如何将 viewController 放入 UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想初始化 5 个视图控制器,当我的应用加载时,我希望能够在 UIScrollView 中轻弹这些视图控制器.

I want to initialize 5 viewController's that I want to be able to flick between in a UIScrollView, when my app loads.

推荐答案

以下是如何执行此操作的示例:

Here is an example of how you can do this:

- (void)viewDidLoad 
{

    //standard UIScrollView is added
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    [self.view addSubview:scrollView];

    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(320*2, 460); //this must be the appropriate size!

    //required to keep your view controllers around
    controllers = [[NSMutableArray alloc] initWithCapacity:0];

    //just adding two controllers
    LabeledViewController *one = [[LabeledViewController alloc] initWithPosition:0 text:@"one"];

    [scrollView addSubview:one.view];
    [controllers addObject:one];

    LabeledViewController *two = [[LabeledViewController alloc] initWithPosition:1 text:@"two"];
    [scrollView addSubview:two.view];
    [controllers addObject:two];
}

LabeledViewController 非常简单,但您可以随心所欲地添加:

LabeledViewController is pretty simple, but you can add as much to it as you want:

@implementation LabeledViewController

- (id)initWithPosition:(NSInteger)position text:(NSString*)text 
{
    if (self = [super init]) {
        myPosition = position;
        myText = [text retain];
    }
    return self;
}


- (void)viewDidLoad 
{
    //this will setup the position in the UIScrollView
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(320*myPosition, 0, 320, 460)];
    self.view = view;

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 320, 50)];
    label.text = myText;

    [self.view addSubview:label];
}

这篇关于如何将 viewController 放入 UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆