xcode 使用 iOS5 的故事板时如何设计 UIScrollView 的子视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8204176/
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 design subviews for UIScrollView when using storyboards for iOS5?
提问by Tim
I am using the example from this question: How to combine UIScrollview with UIPagecontrol to show different views?to add a UIScrollView
containing multiple subviews to a page in my iPad app.
我正在使用这个问题中的示例:如何将 UIScrollview 与 UIPagecontrol 结合以显示不同的视图?添加一个UIScrollView
包含多个子视图到我 iPad 应用程序中的页面。
My app has one large view at the top then a small box at the bottom. The box at the bottom is the UIScrollView; the user will swipe left and right to bring up boxes for various functions in that small section. The large view at the top never changes, so I don't think segues are appropriate.
我的应用程序顶部有一个大视图,底部有一个小框。底部的框是UIScrollView;用户将左右滑动以在该小部分中调出各种功能的框。顶部的大视图永远不会改变,所以我认为 segues 是不合适的。
I have a storyboard with a single View Controller Scene in it. While I can add views to this I can't design 'off screen' views; in other words, there's nowhere I can draw each of my subviews. How can I do this? Do I need to make separate XIBs for each subview? If so, how do I load them in? Or, should I instead make my scrollview (subview width * subview count
) wide and draw each view adjacent to the previous one? This is my first iOS app so I may well have some things backwards.
我有一个故事板,其中有一个视图控制器场景。虽然我可以为此添加视图,但我无法设计“屏幕外”视图;换句话说,我无处可画我的每个子视图。我怎样才能做到这一点?我是否需要为每个子视图制作单独的 XIB?如果是这样,我如何加载它们?或者,我应该让我的滚动视图 ( subview width * subview count
) 变宽并绘制与前一个相邻的每个视图?这是我的第一个 iOS 应用程序,所以我可能会有一些倒退的东西。
Thanks
谢谢
Tim
蒂姆
回答by Scott Sherwood
Currently storyboards are not ideal for doing this however you should not use separate nib files. Instead you should pull on other viewControllers and setup each of their views as required. You must remember to set the identifier of the view controller in interface builder and then you can load the view like so,
目前故事板不适合这样做,但是您不应该使用单独的 nib 文件。相反,您应该使用其他 viewController 并根据需要设置它们的每个视图。您必须记住在界面构建器中设置视图控制器的标识符,然后您可以像这样加载视图,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifierInIB"];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewControllerIdentifierInIB"];
Now you have the view controller you can get access to the view and add it wherever you would like.
现在你有了视图控制器,你可以访问视图并将它添加到任何你想要的地方。
Note: To change the size of your view you will have to select the view controller and change its Size in the attribute inspector in interface builder to freeform. Then you will be able to change the size of the view.
注意:要更改视图的大小,您必须选择视图控制器并在界面构建器的属性检查器中将其大小更改为自由格式。然后您将能够更改视图的大小。
回答by Pedro Borges
I also found that, but eventually you lose the purpose of storyboarding which is to present you with the full-flow of your graphical application.
我也发现了这一点,但最终你失去了故事板的目的,即向你展示图形应用程序的完整流程。
What I really feel lacking here is a new cocoa object i.e. UIPagedScrollController that when added to the storyboard would have it's own segue transition similar to UIToolBarController and UINavigationController but would be comprised of both the UIPageControl and UIScrollView needed to make the magic.
我真正觉得这里缺少的是一个新的可可对象,即 UIPagedScrollController,当它添加到故事板时,它会拥有自己的类似于 UIToolBarController 和 UINavigationController 的转场过渡,但将包含制作魔法所需的 UIPageControl 和 UIScrollView。
回答by Tim
I eventually found that I could create the subviews in separate NIBs. In the subview NIBs I set File's Owner's identity to my main view controller's class and add an outlet to the view controller for the subview. I then link the subview in the NIB to that outlet in File's Owner.
我最终发现我可以在单独的 NIB 中创建子视图。在子视图 NIB 中,我将文件所有者的身份设置为我的主视图控制器的类,并为子视图的视图控制器添加一个插座。然后我将 NIB 中的子视图链接到 File's Owner 中的那个出口。
Finally in the main view controller's viewDidLoad
I call this for each subview NIB:
最后在主视图控制器中,viewDidLoad
我为每个子视图 NIB 调用它:
[[NSBundle mainBundle] loadNibNamed:@"MySubView" owner:self options:nil];