如何创建自定义UIView? [英] How to create a custom UIView?

查看:83
本文介绍了如何创建自定义UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个UIView子类和相应的xib文件,在其中放置了一些UILabel和UIImageViews.然后,我想将此自定义UIView的多个副本放入UIViewController中.

I've created a UIView subclass and corresponding xib file where I've laid out some UILabels and UIImageViews. I want to then put multiple copies of this custom UIView into a UIViewController.

当我这样做时,它们在界面生成器中显示为空白,并且在应用加载时不显示.为了使这项工作有效,我需要在UIView子类上实现哪些方法?

When I do that they appear blank in interface builder and don't appear when the app loads. What methods do I need to implement on the UIView subclass to make this work?

推荐答案

到目前为止,最简单的方法是创建一个将File的Owner设置为NSObject的笔尖,其中包含一个包含布局元素的视图.

The easiest method by far is to create a nib with File's Owner set to NSObject, containing one view containing your layout element.

然后

NSArray *arr = [[NSBundle mainBundle] loadNibNamed:@"MyView" owner:nil options:nil];
UIView *fv = [[arr objectAtIndex:0] retain];

// now you have the view, do something with it:
fv.center = CGPointMake(100,100);
[self.view addSubview:fv];

无需与文件所有者进行任何操作;甚至在这里设置为零.

No need to do anything with the file's owner; it is even set to nil here.

这篇关于如何创建自定义UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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