ios 故事板中视图控制器外部的 UIView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11386111/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:10:58  来源:igfitidea点击:

UIView outside of a View Controller in a Storyboard

iosxcodecocoa-touchstoryboard

提问by KPM

I'm stuck with a question I can't find an answer to in the official documentation, on Stackoverflow or on Google.

我遇到了一个在官方文档、Stackoverflow 或 Google 上找不到答案的问题。

I'm an experienced iOS developer but new to Storyboards. I'd like to instantiate a view on the current viewcontroller, a view that, in a NIB, I would draw outside the viewController and attach an outlet to to refer to it in code and call something like addSubview: or so. It's useful to draw an overlay or, in my current case, a callout view for use in a MapView delegate. But I can't figure out how I can do this in a Storyboard.

我是一位经验丰富的 iOS 开发人员,但不熟悉 Storyboard。我想在当前视图控制器上实例化一个视图,一个视图,在 NIB 中,我会在 viewController 之外绘制并附加一个插座以在代码中引用它并调用类似 addSubview: 之类的东西。绘制叠加层或在我当前的情况下绘制用于 MapView 委托的标注视图很有用。但我不知道如何在故事板中做到这一点。

I can't seem to have a view outside of a viewcontroller, and views inside the viewcontroller either have to be subviews of the controller's view or don't be visible in Interface Builder at all (which is okay when you drag a view composed outside, in a traditional NIB, but quite a problem if you need to tweak even the least detail of it).

我似乎无法在视图控制器之外拥有视图,并且视图控制器内部的视图要么必须是控制器视图的子视图,要么在 Interface Builder 中根本不可见(当您拖动外部组成的视图时,这是可以的,在传统的 NIB 中,但如果您需要调整它的最细微的细节,这将是一个相当大的问题)。

Any hint on what to do or where to look? Thanks.

关于做什么或在哪里看的任何提示?谢谢。

采纳答案by KPM

It appears there is no way to do it. See Jason's commentand this questionaddressing the same problem, though in a different context.

似乎没有办法做到这一点。请参阅Jason 的评论此问题解决相同的问题,但在不同的上下文中。

However, I realised earlier when thinking about the implications of my problem — as I began to explain in my previous commentbefore deciding it would be worth a full answer — it makes sense to separate the design of a custom view from the design of an application flow. So the fact that it's not possible to design custom views in Storyboards may very well be... by design.

然而,在考虑我的问题的含义时,我更早意识到——正如我在我之前的评论中开始解释的那样,然后决定它是否值得一个完整的答案——将自定义视图的设计与应用程序的设计分开是有意义的流动。因此,不可能在 Storyboards 中设计自定义视图的事实很可能是……设计使然。

A custom view (which is a reusable component) is very different from a view controller (which represents a scene in your application storyboard). It's the same as a character and a scene, in a movie. You don't want to design your character in your scene, even if you happen to use that character in only one scene. It's fortuitous if you use it in only one scene, but not essential. So it has to be separate.

自定义视图(它是一个可重用的组件)与视图控制器(它代表应用程序故事板中的一个场景)非常不同。这与电影中的角色和场景相同。您不想在场景中设计角色,即使您碰巧只在一个场景中使用该角色。如果你只在一个场景中使用它是偶然的,但不是必需的。所以必须分开。

The fact that you want to use Interface Builder to design your custom view is only an implementation detailthat should be encapsulated in your class. The application storyboard has no need of knowing, even less managing, how you deal with the particular implementation of your reusable view.

您想使用 Interface Builder 来设计您的自定义视图这一事实只是一个应该封装在您的类中的实现细节。应用程序故事板不需要知道,更不用说管理,您如何处理可重用视图的特定实现。

Hmm, I wish I had more of this brand of interrogation :)

嗯,我希望我有更多这种类型的审讯:)

回答by Josejulio

You can use a Container View, is like a regular view(You can assign a IBOutlet UIView) but you can define it outside of your UIViewController.

您可以使用容器视图,就像常规视图一样(您可以分配一个 IBOutlet UIView),但您可以在 UIViewController 之外定义它。

The "container view" is outside the view, but i can assign it to a IBOutlet UIViewYou can design it

“容器视图”在视图之外,但我可以将其分配给 IBOutlet UIView你可以设计它

回答by Joel Wehr

I was wondering the same thing myself, and actually was able to get this to happen, though after messing around, its gone. I have a Nav Controller, a View Controller and two views. I had developed the views in a separate nib, and imported them to this project. While was was connecting buttons, the view I was working in suddenly appeared next to my View Controller. Unfortunately, while trying to get the other view to appear, I somehow lost it, and dont know how to get it back. It doesnt seem like you can drag a view to the storyboard, only into a view controller. However, if I was able to do it once, its got to be reproduceable.

我自己也在想同样的事情,实际上能够做到这一点,尽管在搞砸之后,它消失了。我有一个导航控制器、一个视图控制器和两个视图。我在一个单独的笔尖中开发了这些视图,并将它们导入到这个项目中。在连接按钮时,我正在使用的视图突然出现在我的视图控制器旁边。不幸的是,在试图让另一个视图出现时,我不知何故丢失了它,并且不知道如何恢复它。似乎您不能将视图拖到故事板中,只能拖到视图控制器中。但是,如果我能够做到一次,它就必须是可复制的。