xcode 在情节提要中创建独立视图以编程方式使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14139564/
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
Create a standalone view in storyboard to use programmatically
提问by Yarek T
I'm trying to create and design a UIView
using a storyboard but include it in a UIActionSheet
programmatically. This is basically to avoid using CoreGraphics positioning functions with pixels
我正在尝试UIView
使用故事板创建和设计一个,但以UIActionSheet
编程方式将其包含在一个中。这基本上是为了避免使用带有像素的 CoreGraphics 定位函数
In old xcode I remember that it was possible to drag a UIView onto the nib without any controllers.
在旧的 xcode 中,我记得可以在没有任何控制器的情况下将 UIView 拖到笔尖上。
The view obviously has to be connected to the class, so it would have an IBOutlet, but not being added to the self.view
视图显然必须连接到类,所以它会有一个 IBOutlet,但不会被添加到 self.view
One thing that makes me feel like this should be possible is that if you drag a UIView into the controller black bar in storyboard it pops into place like so:
让我觉得这应该是可能的一件事是,如果您将 UIView 拖动到故事板中的控制器黑条中,它会像这样弹出到位:
But its not shown on the screen itself. What is this feature for? Can I somehow open up this view and design it a bit?
但它没有显示在屏幕上。这个功能有什么用?我可以以某种方式打开这个视图并设计一下吗?
回答by eric
Just create a new .xib file.
只需创建一个新的 .xib 文件。
- Right Click somewhere in the Navigator Area and select 'New File...'.
- Select 'User Interface' from the list on the right and Select 'View'.
- Click 'Next', 'Next', give your new view a name, and 'Create'.
- A new .xib fill will be added to your project.
- Double clicking on the new .xib file and it opens in Interface Builder (not Storyboard).
- Edit/Design your View to your liking.
- 右键单击导航器区域中的某处,然后选择“新建文件...”。
- 从右侧的列表中选择“用户界面”,然后选择“查看”。
- 单击“下一步”、“下一步”,为您的新视图命名,然后单击“创建”。
- 新的 .xib 填充将添加到您的项目中。
- 双击新的 .xib 文件,它会在 Interface Builder(不是 Storyboard)中打开。
- 根据您的喜好编辑/设计您的视图。
Then after you have your new view (.xib) in Interface Builder, it's a simple matter of creating a new subclass of UIView (ex. MyView), switching the class of your new view (.xib) to MyView, creating an instance of MyView in your controller, and adding it as a subview to your other view.
然后在 Interface Builder 中创建新视图 (.xib) 后,创建一个新的 UIView 子类(例如 MyView),将新视图(.xib)的类切换到 MyView,创建一个实例MyView 在您的控制器中,并将其作为子视图添加到您的其他视图中。
*And to answer your question about that little black barat the bottom, it's called the 'Dock', and it's just a mini representation of the top-level documents of your scene. The dock is convenient for quickly dragging/dropping icons onto and making connections. See apple's storyboard description here. Ray Wenderlich has an easy to follow tutorial on storyboards here.
*为了回答您关于底部那个小黑条的问题,它被称为“Dock”,它只是您场景的顶级文档的一个迷你表示。Dock 便于快速拖放图标并建立连接。在这里查看苹果的故事板描述。雷Wenderlich有一个易于遵循教程故事板在这里。
回答by DBD
You cannot have a UIView
outside of a UIViewController
on a storyboard. I'm guessing it's because the storyboard would have no idea how to identify or instantiate with the current API. It is something I've had a use for myself. The solution is just use a XIB for the one UIView
and load it up programmatically (just like used to do). I've found using a storyboard for most items and couple XIBs for re-usable views across multiple view controllers do work nicely together.
故事板上不能有 a 的UIView
外部UIViewController
。我猜这是因为故事板不知道如何识别或实例化当前的 API。这是我对自己有用的东西。解决方案是只使用 XIB 并以UIView
编程方式加载它(就像以前一样)。我发现对大多数项目使用故事板,并为跨多个视图控制器的可重用视图使用几个 XIB 可以很好地协同工作。
Here is some code I use to load a XIB as part of a custom object with the object gets initialized.
这是我用来加载 XIB 作为自定义对象的一部分的一些代码,该对象已初始化。
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[[[NSBundle mainBundle] loadNibNamed:@"BannerView" owner:self options:nil] objectAtIndex:0];
[self addSubview:self.view];
self.frame = self.view.frame;
}
return self;
}
As for dragging views down into the black bar on the storyboards. Those views are still part of the UIViewController
, but they aren't a `subview' of the top level view. I think the document outline shows the hierarchy nicely.
至于将视图向下拖动到故事板上的黑条中。这些视图仍然是 的一部分UIViewController
,但它们不是顶级视图的“子视图”。我认为文档大纲很好地显示了层次结构。
The following view has 2.1.1 View, 2.1.2 View, etc outside of my main view hierarchy because they aren't subviews of my main view. The result is, they won't be displayed by default. I do have IBOutlets
setup and I conditionally add/remove them from my main view hierarchy using the standard addSubview:
and removeFromSuperview
.
以下视图在我的主视图层次结构之外具有 2.1.1 视图、2.1.2 视图等,因为它们不是我的主视图的子视图。结果是,默认情况下它们不会显示。我确实进行了IBOutlets
设置,并且使用标准addSubview:
和removeFromSuperview
.