How to create a UIScrollView Programmatically?(如何以编程方式创建 UIScrollView?)
问题描述
好的,这里的关键是我根本没有使用 IB,因为我正在使用的视图是通过编程方式创建的.UIView
覆盖了屏幕的下半部分,上面有一堆按钮.但是,我想在 UIView
中添加更多按钮,而不使其变得更大.为此,我想在视图中创建一个 UIScrollView
,这将允许我在屏幕外添加更多按钮,以便用户可以滚动到它们.我认为这就是它的工作原理.
Alright, so the key here is I'm not using IB at all, because the View I'm working with is created programmatically. The UIView
covers the lower half the screen, and has a bunch of buttons on it. However, I want to add more buttons to the UIView
, without making it any larger. To do so, I want to make a UIScrollView
inside the view, which will allow me to add more buttons off screen so the user can scroll to them. I think that's how it works.
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
并将其添加到视图中,然后向其中添加按钮.
The first part of the code iniates my UIView
, which works great, but I can't figure out how to make the UIScrollView
programmatically and add it to the view, and then add the buttons to it.
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];
当我这样做时,按钮从我的屏幕上消失了.那么我该如何正确地做到这一点呢?感谢您的帮助!
When I did that, the button simply disappeared off my screen. So How do I do this correctly? Thank you for your help!
推荐答案
代替:
UIScrollView *scroll = [UIScrollView alloc];
执行此操作(将框架设置为您希望滚动视图的大小):
do this (setting the frame to however big you want the scroll view to be):
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:...];
这篇关于如何以编程方式创建 UIScrollView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何以编程方式创建 UIScrollView?
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01