在 Storyboard 中编辑自定义 UIView 子类

时间:2023-03-24
本文介绍了在 Storyboard 中编辑自定义 UIView 子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个应用程序,它使用 UIScrollView 在多个动态生成的 UIView 之间进行分页.现在,我必须以编程方式设置这些 UIView 的布局和 ui 元素,但我想知道是否有任何方法可以在 storyboard 中编辑 UIView 子类的界面(独立于视图控制器).

I have an application that uses a UIScrollView to page between a number of dynamically generated UIViews. 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).

提前致谢.

仅供参考,我在 iOS 5 中使用 Xcode 4.2.

Just for reference, I'm using Xcode 4.2 with iOS 5.

推荐答案

更新

从 Xcode 7 开始,您可以在情节提要中编辑独立视图.将视图从对象库拖到视图控制器的标题栏.Interface Builder 将在视图控制器主视图上方的画布上单独显示独立视图.示例:

UPDATE

As of Xcode 7, you can edit a standalone view in a storyboard. 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. 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.

在情节提要中没有方便的方法来做到这一点.

There is no convenient way to do this in a storyboard.

您仍然可以在使用情节提要的项目中创建 nib.因此,您可以为每个视图创建一个 nib,并从您的代码中加载它们:

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];

这篇关于在 Storyboard 中编辑自定义 UIView 子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:不推荐使用发布方法 下一篇:由于“进程崩溃",检测运行失败.经过长时间的测试执行

相关文章