簡體   English   中英

如何將viewController放入UIScrollView

[英]How do I put viewController's into a UIScrollView

我想初始化5個viewController,我希望能夠在我的應用程序加載時在UIScrollView中輕彈。

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

- (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非常簡單,但你可以根據需要添加它:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM