使用Storyboard自定义视图 [英] Custom views with Storyboard

查看:150
本文介绍了使用Storyboard自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在复杂的屏幕(视图控制器)中,我曾经把整个东西分成小块(我称它们为小部件)。这些小部件基本上由 MyWidget.h MyWidget.m 文件以及 MyWidget.xib 文件,其中根元素是 UIView ,MyWidget类是UIView的文件所有者。在这个小部件的init中,我做了一个 loadNibNamed

In complex screens (View Controllers) I used to separate the whole thing in smaller pieces (I call them widgets). These widgets consist basically of a MyWidget.h and a MyWidget.m file as well as a MyWidget.xib file, where the root element is a UIView and the MyWidget class is the File Owner of the UIView. In the init of this widget I do a loadNibNamed.

在我的View Controller中我做了一个 [[MyWidget alloc] init] ,我将其作为子视图添加到View的Controller主视图中。到目前为止,这完全可行。

In my View Controller I then do a [[MyWidget alloc] init], which I add to View's Controller main view as a sub view. This, so far, works perfectly.

我现在想知道,如何对故事板做同样的事情,因为我无法真正开始拖动 UIView 某处,我总是要以 UIViewController 开头,我不想这样做。

I'm now wondering, how to do the same with storyboard, because I cannot really start to drag in a UIView somewhere, I always have to start with an UIViewController, which I don't want to.

如果使用Storyboard无法做到这一点,我可以通过使用故事板为我的主屏幕和segue以旧方式,并使用单独的.xib文件来定义自定义视图?

If there is no possible way doing this with a Storyboard, can I simply do it the old way, by using the Storyboard for my main screens and segues, and use a separate .xib file to define custom views?

推荐答案

将小部件/视图放在单独的.xib文件中是有效的,尤其适用于您可能需要参考的文件来自多个View控制器的相同视图。

Putting the widget/view in a separate .xib file works, and is appropriate especially if you might want to reference that same view from multiple View Controllers.

但是,有时您确实希望在同一个故事板中看到其他视图/小部件,这是可能的。以下是您的操作方法:

However, sometimes you do want to see the additional view/widget within the same storyboard, and it is possible. Here's how you do it:


  1. 在IB中选择您的视图控制器(单击视图下方的黑色条),然后拖动从对象库进入黑条的UIView:

  1. Select your view controller in IB (click on the black bar below the view), then drag a UIView from the Object Library into the black bar:

当视图位于黑色条形图中时,它会像IB中的任何其他视图一样进行实例化,但不会添加到您的视图中层次结构,直到你在代码中这样做。如有必要,更改视图的类以匹配您自己的子类:

When a view is in the black bar, it's instantiated like any other view in IB but just isn't added to your view hierarchy until you do so in code. Change the view's class to match your own subclass if necessary:

您可以将它连接到视图控制器,就像连接任何其他视图一样:

You can hook it up to your view controller like you would hook up any other view:

添加的视图显示在文档大纲中你也可以在那里连接动作和引用:

The added view shows up in your Document Outline and you can hook up actions and references there too:






现在,问题仍然存在无论您尝试单击或双击多少次,都无法实际看到视图,这会使将其置于同一故事板中的全部目的失败。幸运的是,我知道有两种解决方法。


Now, the problem that remains is that you can't actually see the view no matter how many times you try to click or double click, which would defeat the whole purpose of putting it in the same storyboard. Fortunately there are two workarounds that I know of.

第一种解决方法是将视图从黑条拖回视图控制器的视图,编辑它,然后拖动它一旦你完成,就回到黑色吧台。这很麻烦但很可靠。

The first workaround is to drag the view from the black bar back into your view controller's view, edit it, then drag it back into the black bar once you're done. This is troublesome but reliable.

另一种解决方法更挑剔,但我更喜欢它,因为它可以让我同时看到我的所有观点:

The other workaround is more finicky, but I prefer it because it lets me see all my views at the same time:


  1. 将UITableView从对象库拖到新添加的视图中。

  2. 然后将UITableViewCell拖入UITableView。

  1. Drag a UITableView from the Object Library into your newly added view.
  2. Then drag a UITableViewCell into that UITableView.

一旦你这样做,你的视图会在旁边神奇地弹出,但你有一个你不想要的UITableView。您可以将其大小调整为0x0,也可以将其删除,并且您的UIView将(通常)仍然可见。

Once you do that, your view pops out magically by the side, but you have a UITableView that you don't want. You can either resize that to 0x0, or you can delete it and your UIView will (usually) still stay visible.

第二种方法适用于UIViews,但UIToolbars不太好,UIButtons是不可能的,所以当你需要包含许多不同的子视图时,我发现的最干净的解决方案是附加一个辅助UIView将您的视图控制器视为永远不会显示的容器,将所有辅助视图放在那里,并使用UITableViewCell技巧使所有内容都可见。我将我的虚拟UITableView调整为0x0以使其不可见。以下是它们在一起的截图:

The second method works for UIViews but not so well for UIToolbars and is impossible for UIButtons, so the cleanest solution I've found when you need to include lots of different subviews is to attach a single secondary UIView to your view controller as a container that never gets shown, put all your secondary views in there, and use the UITableViewCell trick to make everything visible. I resize my dummy UITableView to 0x0 to make that invisible. Here's a screenshot of how it all looks like together:

这篇关于使用Storyboard自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆