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

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

How add a custom UIView to a storyboard scene

iphoneiosxcodeuiviewstoryboard

提问by Nick Turner

I created a UIView with an image view inside it. Gave it a name of "XXXMyView".

我创建了一个 UIView,里面有一个图像视图。给它起一个名字“XXXMyView”。

in my storyboard view controller, i dragged a UIView on to the View Controller (scene) and then set its class to XXXMyView.

在我的故事板视图控制器中,我将一个 UIView 拖到视图控制器(场景)上,然后将其类设置为 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?

我怎样才能看到我在显示图像视图的屏幕上创建的视图?

采纳答案by rdurand

What you should do is create an empty UIView in your storyboard. Drag a UIImageView on your view.

您应该做的是在故事板中创建一个空的 UIView。在您的视图上拖动 UIImageView。

Then create a subclass of 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. ctrl-drag from your UIImageView to your .h (between @interfaceand @end). Give it a name. You created a link between your visual representation of the UIImageView and your code. You can now access it from your .m file.

然后创建一个 UIView 的子类。在故事板中,设置视图的类以匹配您最近创建的类。打开编辑器窗口(主项目窗口的右上角,“编辑器”部分的中间按钮)。将 XXXView.h 放在左侧窗口,将故事板放在右侧。ctrl-drag 从你的 UIImageView 到你的 .h (在@interface和之间@end)。给它一个名字。您在 UIImageView 的可视化表示和代码之间创建了一个链接。您现在可以从您的 .m 文件访问它。

Ray Wenderlichhas a pretty neat tutorial on storyboards. Here are Part 1, and Part 2.

Ray Wenderlich有一个非常简洁的故事板教程。这是第 1部分第 2 部分

回答by Fellowsoft

I think your looking at this the wrong way (from what I understand)

我认为你以错误的方式看待这个(据我所知)

  1. See the UIViewControlleras the "main" view and use it's class to manipulate the subviews (the UIViewand UIImageView)

  2. Link your UIViewand UIImageViewas objects inside your UIViewControllerclass and use those objects to do what you need to (add new subviews dynamically or edit properties etc.)

  3. You can add subviews to a UIViewby using [yourViewObject addSubview: (UIView *)]. Don't be fooled by the (UIView *)you can use any subclass of UIViewas an argument. (e.g. an UIImageViewobject)

  1. UIViewController视为“主”视图并使用它的类来操作子视图(UIViewUIImageView

  2. 将您的UIViewUIImageView作为UIViewController类中的对象链接起来,并使用这些对象来执行您需要的操作(动态添加新子视图或编辑属性等)

  3. 您可以UIView使用向 a 添加子视图[yourViewObject addSubview: (UIView *)]。不要被(UIView *)你可以使用任何子类UIView作为参数所迷惑。(例如一个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'stutorials on storyboarding as rdurandsugested. Here's the link again Part 1.

如果你不知道如何链接(或基准)从故事板的类的对象我会sugest合作,通过雷Wenderlich的教程在故事板作为rdurandsugested。这是第 1 部分的链接。

回答by Christian R

Perhaps you need to set the "view" outlet of the UIViewController to the new XXXMyView.

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