[英]On iOS, if we use Interface Builder for the UIViewController's view, how do we implement a drawRect for it?
If we use Interface Builder to edit the content in the view, then how do we add a drawRect
to that UIView
object? 如果我们使用Interface Builder编辑视图中的内容,那么如何为该
UIView
对象添加drawRect
? (Probably we want a new class FooView
that subclasses UIView
-- but how will the .xib
content be placed on this view?) (也许我们想要一个新类
FooView
来UIView
,但是.xib
内容将如何放置在此视图上?)
Adding "content" to the view controller's view in the xib just involves adding subviews - these are not affected by drawRect:
. 在xib中向视图控制器的视图添加“内容”仅涉及添加子视图-这些不受
drawRect:
影响。
That said, to set a custom class as the main view
property of a view controller, just select the view in the xib, go to the identity inspector, and change the class from UIView to your custom class. 也就是说,要将自定义类设置为视图控制器的主要
view
属性,只需在xib中选择视图,转到身份检查器,然后将类从UIView更改为自定义类。
At beginning i want to point that i have done something like this(subclassing) with UIButton and since UIButton is inherited from UIView this must be possible with UIView too. 首先,我想指出的是,我已经使用UIButton完成了类似的操作(子类化),并且由于UIButton是从UIView继承的,因此UIView也必须可行。
(after subclassing UIView) create an instance of your subclass on your view controller and add this instance to ViewController as subview. (在对UIView进行子类化之后)在视图控制器上创建子类的实例,并将该实例作为子视图添加到ViewController。 At this point you can call your specific drawRect: method, which you have declared on your subclass
此时,您可以调用特定的drawRect:方法,该方法已在子类上声明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.