gpt4 book ai didi

iphone - 如何将 viewController 放入 UIScrollView 中

转载 作者:行者123 更新时间:2023-12-03 18:50:26 25 4
gpt4 key购买 nike

我想初始化 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];
}

关于iphone - 如何将 viewController 放入 UIScrollView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1990658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com