iphone - 如何以编程方式创建 UIScrollView?

好的,这里的关键是我根本没有使用 IB,因为我正在使用的 View 是通过编程方式创建的。 UIView 覆盖了屏幕的下半部分,上面有一堆按钮。但是,我想在 UIView 中添加更多按钮,而不使其变得更大。为此,我想在 View 中创建一个 UIScrollView,这将允许我在屏幕外添加更多按钮,以便用户可以滚动到它们。我认为这就是它的工作原理。

self.manaView = [[[UIView alloc] initWithFrame:frame] autorelease];
self.manaView.backgroundColor = [UIColor purpleColor];

UIScrollView *scroll = [UIScrollView alloc];
scroll.contentSize = CGSizeMake(320, 400);
scroll.showsHorizontalScrollIndicator = YES;
[self.manaView addSubview:scroll];

代码的第一部分启动了我的 UIView,效果很好,但我不知道如何以编程方式制作 UIScrollView 并将其添加到 View 中,然后向其中添加按钮。

UIButton *ret2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
ret2.tag = 102;
ret2.frame = CGRectMake(255, 5, 60, 50);
[ret2 setTitle:@"Return" forState:UIControlStateNormal];
[ret2 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:ret2];

当我这样做时,按钮从我的屏幕上消失了。那么我该如何正确地做到这一点呢?感谢您的帮助!

最佳答案

代替:

UIScrollView *scroll = [UIScrollView alloc];

执行此操作(将框架设置为您希望 ScrollView 的大小):

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:...];

https://stackoverflow.com/questions/2998336/

相关文章:

objective-c - 处理由于 iOS 通讯录 API 中的链接卡导致的重复联系人

iphone - Objective-C - 将 float 转换为字符串的其他方法

iphone - Objective-C 实现文件中方法名后的分号

ios - 如何在 iOS 中使用 'Container View'?

objective-c - 为什么不鼓励使用伞式框架?

ios - 切换隐私设置将杀死应用程序

ios - UIImagePickerController 相机 View 在 iOS 8 上奇怪地

ios - 带有 AVPlayer 的多个视频

objective-c - 在子类中覆盖初始化

iphone - Objective-C 中的实例变量是否默认设置为 nil?