ios 如何将对象添加到从 Storyboard 扩展到 UIView 之外的 UIScrollView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16889123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add objects to a UIScrollView that extend beyond UIView from Storyboard?
提问by jake9115
I want to have several buttons and other objects in a long UIScrollView
in my app. In storyboard, I added a UIScrollView
to fill the entre view, and then created an IBOutlet
in my .h file. I synthesized the scroller in my .m file, and then used the following code to start the scroller:
我想UIScrollView
在我的应用程序中有几个按钮和其他对象。在故事板中,我添加了一个UIScrollView
来填充主视图,然后IBOutlet
在我的 .h 文件中创建了一个。我在我的 .m 文件中合成了滚动条,然后使用以下代码启动滚动条:
@synthesize scroller = _scroller;
- (void)viewDidLoad
{
[super viewDidLoad];
[_scroller setScrollEnabled:YES];
[_scroller setContentSize:CGSizeMake(640, 3000)];
}
So now I need to know how to actually add things, such as buttons, to the area of the scroller that extends below what you can see in the view. My problem is that as I add butons to my view in storyboard, I can only add things to what you can see in the view, and therefore need to know how to add buttons to part that I will scroll to!
所以现在我需要知道如何将按钮之类的东西实际添加到滚动条的区域,该区域延伸到您在视图中可以看到的区域下方。我的问题是,当我在情节提要中向视图添加按钮时,我只能向视图中可以看到的内容添加内容,因此需要知道如何将按钮添加到我将滚动到的部分!
Hopefully this is clear. Thanks for all your help!
希望这很清楚。感谢你的帮助!
回答by rob mayoff
UPDATE
更新
I have posted a screencast that walks through this technique step-by-step.
ORIGINAL
原来的
The easiest way to handle this is simply to make the view in your storyboard taller. When the app runs, any of the normal container view controllers (UINavigationController
, UITabBarController
, UISplitViewController
, or even UIViewController
when it's the root view controller of its window) will resize the view to fit on the screen and scroll.
处理这个问题的最简单方法就是让故事板中的视图更高。当应用程序运行时,任何普通的容器视图控制器(UINavigationController
、UITabBarController
、UISplitViewController
,甚至UIViewController
当它是其窗口的根视图控制器时)都会调整视图的大小以适应屏幕并滚动。
Here's an example of how to set it up in Xcode:
以下是如何在 Xcode 中设置它的示例:
I changed the view controller's size from “Inferred” to “Freeform”. Then I changed its view's height from 460 to 800. (By the way, control-shift-click gives you a menu of all objects under the cursor.)
我将视图控制器的大小从“Inferred”更改为“Freeform”。然后我将其视图的高度从 460 更改为 800。(顺便说一下,control-shift-click 为您提供了光标下所有对象的菜单。)
Here's what happens when I run it in the simulator:
这是我在模拟器中运行它时发生的情况:
As you can see, the view hierarchy was resized to fit the screen, but the subviews of the UIScrollView
weren't repositioned, and the scroll view set its content size appropriately. (That may only work properly with autolayout, though...)
如您所见,视图层次结构已调整大小以适应屏幕,但 的子视图UIScrollView
未重新定位,并且滚动视图适当地设置了其内容大小。(不过,这可能只适用于自动布局……)
回答by ggrana
So, an example to you how to add a Button:
所以,一个例子给你如何添加一个按钮:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"Cool title" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(50, 700, 100, 100)];
[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[_scroller addSubview:btn];
You just need to set the frame of the view that you want to add and after that add it as a subview in your scroll.
您只需要设置要添加的视图的框架,然后将其添加为滚动中的子视图。
回答by Michael Henry
You can you use the Size inspector(Left part of xcode) in order to position it. or reposition it programmatically.
您可以使用大小检查器(xcode 的左侧部分)来定位它。或以编程方式重新定位它。
Cheers, Kel
干杯,凯尔