简体   繁体   English

在 Storyboard 中编辑自定义 UIView 子类

[英]Editing Custom UIView Subclasses in Storyboard

I have an application that uses a UIScrollView to page between a number of dynamically generated UIViews.我有一个应用程序,它使用 UIScrollView 在多个动态生成的 UIView 之间进行分页。 Right now, I have to programmatically set the layout and ui elements of these UIViews, but I was wondering if there is any way to edit the interface of a subclass of UIView in storyboard (independent of a view controller).现在,我必须以编程方式设置这些 UIView 的布局和 ui 元素,但我想知道是否有任何方法可以编辑 storyboard 中 UIView 子类的界面(独立于视图控制器)。

Thanks in advance.提前致谢。

Edit: Just for reference, I'm using Xcode 4.2 with iOS 5.编辑:仅供参考,我在 iOS 5 中使用 Xcode 4.2。

UPDATE更新

As of Xcode 7, you can edit a standalone view in a storyboard.从 Xcode 7 开始,您可以在故事板中编辑独立视图。 Drag a view from the Object Library to the header bar of a view controller.将视图从对象库拖到视图控制器的标题栏。 Interface Builder will show the standalone view separately on the canvas above the view controller's main view. Interface Builder 将在视图控制器主视图上方的画布上单独显示独立视图。 Example:例子:

故事板中的独立视图

You will probably want to create an outlet from your view controller to the standalone view.您可能希望创建一个从视图控制器到独立视图的插座。 The standalone view will only be accessible from the view controller that contains it.独立视图只能从包含它的视图控制器访问。

Note that you get one instance of the standalone view when you load the view controller.请注意,当您加载视图控制器时,您将获得一个独立视图的实例。 If you need multiple instances, there's no particularly good way to get them.如果您需要多个实例,则没有特别好的方法来获取它们。

ORIGINAL原来的

There is no convenient way to do this in a storyboard.在故事板中没有方便的方法来做到这一点。

You can still create nibs in a project that uses a storyboard.您仍然可以在使用故事板的项目中创建笔尖。 So you can create a nib for each view, and load them from your code:因此,您可以为每个视图创建一个笔尖,并从您的代码中加载它们:

NSString *nibName = [NSString stringWithFormat:@"page%d", pageNumber];
NSArray *nibObjects = [NSBundle.mainBundle loadNibNamed:nibName owner:self options:nil];
UIView *pageView = [nibObjects objectAtIndex:0];
CGSize size = self.scrollView.bounds.size;
pageView.frame = CGRectMake(pageNumber * size.width, 0, size.width, size.height);
[self.scrollView addSubview:pageView];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM