简体   繁体   English

如何将自定义UIView添加到故事板场景

[英]How add a custom UIView to a storyboard scene

I created a UIView with an image view inside it. 我在里面创建了一个带有图像视图的UIView。 Gave it a name of "XXXMyView". 给它起了一个名为“XXXMyView”的名字。

in my storyboard view controller, i dragged a UIView on to the View Controller (scene) and then set its class to XXXMyView. 在我的storyboard视图控制器中,我将UIView拖到View Controller(场景)上,然后将其类设置为XXXMyView。

But when I do I don't see the image view, but's just the background color. 但是当我这样做时,我看不到图像视图,而只是背景颜色。

How can I make it where I see my view I created on the screen that shows the image view? 如何在显示我在显示图像视图的屏幕上创建的视图的位置进行操作?

What you should do is create an empty UIView in your storyboard. 你应该做的是在你的故事板中创建一个空的UIView。 Drag a UIImageView on your view. 在视图上拖动UIImageView。

Then create a subclass of UIView. 然后创建UIView的子类。 In the storyboard, set the class of your view to match your recently created class. 在故事板中,设置视图的类以匹配最近创建的类。 Open the editor window (top right of the main project window, middle button of the "editor" section). 打开编辑器窗口(主项目窗口的右上角,“编辑器”部分的中间按钮)。 Put the XXXView.h on the left window, and your storyboard on the right. 将XXXView.h放在左侧窗口,将您的故事板放在右侧。 ctrl-drag from your UIImageView to your .h (between @interface and @end ). 按住Ctrl键从UIImageView拖动到.h(在@interface@end之间)。 Give it a name. 给它起个名字。 You created a link between your visual representation of the UIImageView and your code. 您在UIImageView的可视化表示与代码之间创建了一个链接。 You can now access it from your .m file. 您现在可以从.m文件中访问它。

Ray Wenderlich has a pretty neat tutorial on storyboards. Ray Wenderlich在故事板上有一个非常简洁的教程。 Here are Part 1 , and Part 2 . 这是第1 部分第2部分

也许您需要将UIViewController的“视图”插座设置为新的XXXMyView。

I think your looking at this the wrong way (from what I understand) 我认为你以错误的方式看待这个(根据我的理解)

  1. See the UIViewController as the "main" view and use it's class to manipulate the subviews (the UIView and UIImageView ) UIViewController视为“主”视图,并使用它的类来操作子视图( UIViewUIImageView

  2. Link your UIView and UIImageView as objects inside your UIViewController class and use those objects to do what you need to (add new subviews dynamically or edit properties etc.) 将您的UIViewUIImageView链接为UIViewController类中的对象,并使用这些对象执行您需要的操作(动态添加新子视图或编辑属性等)

  3. You can add subviews to a UIView by using [yourViewObject addSubview: (UIView *)] . 您可以使用[yourViewObject addSubview: (UIView *)]将子视图添加到UIView Don't be fooled by the (UIView *) you can use any subclass of UIView as an argument. 不要被(UIView *)所愚弄,你可以使用UIView任何子类作为参数。 (eg an UIImageView object) (例如UIImageView对象)

If you don't know how to link (or reference) the objects from the storyboard to the class I would sugest to work through Ray Wenderlich's tutorials on storyboarding as rdurand sugested. 如果您不知道如何将故事板中的对象链接(或引用)到课程,我将通过Ray Wenderlich关于故事板教程作为rdurand sugested来完成工作。 Here's the link again Part 1 . 这是第1部分的链接。

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

相关问题 UIView在情节提要中切换风景和肖像场景的动画? - UIView switching animation for landscape and portrait scene in storyboard? 在 Storyboard 中编辑自定义 UIView 子类 - Editing Custom UIView Subclasses in Storyboard 将我的UIView添加到Storyboard中的ViewController - Add my UIView to a ViewController in Storyboard 使用StoryBoard将“全局” UIView添加到应用程序 - Add a 'global' UIView to an App with StoryBoard ARKit:如何将 UIView 添加到 ARKit 场景? - ARKit: How can I add a UIView to ARKit Scene? 如何添加在故事板中创建的UIView作为Swift中的弹出窗口? - How to add a UIView which is created in storyboard as a pop-up in Swift? 故事板:将其他场景嵌入导航控制器后添加新场景 - storyboard: add a new scene after embedding other scene in navigation controller 如何在自定义 UIView 中添加 UITableView? - How to add UITableView inside custom UIView? 重复使用带有故事板的xib(将UIView添加到UINavigationcontroller) - Reused xib with storyboard (add UIView to UINavigationcontroller) 如何在从界面构建器中的Storyboard场景实例化的视图控制器上调用自定义init? - How do I call a custom init on a view controller instantiated from a Storyboard scene in interface builder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM