存在 Xib 文件的自定义绘图
我有一个视图,我想添加一些自定义绘图。
我知道如何使用未连接到 Nib/Xib 文件的视图执行此操作,
- 您可以在
-drawRect:
方法中编写绘图代码。
但是,如果我使用 -drawRect:
init
视图,
[[MyView alloc] initWithNibName:@"MyView" bundle:[NSBundle mainBundle]];
当然不会被调用。 我尝试在 -viewDidLoad
中执行以下代码,
CGRect rect = [[self view] bounds];
CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ref, 2.0);
CGContextSetRGBStrokeColor(ref, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBFillColor(ref, 0, 0, 0, 0);
CGContextAddRect(ref, CGRectMake(1, 1, rect.size.width - 10, rect.size.height - 10));
CGContextStrokePath(ref);
CGContextDrawPath(ref, kCGPathFillStroke);
但没有绘制任何内容。 有任何想法吗?
I have a view that I want to add some custom drawing to.
I know how to do this with a View that isn't connected to a Nib/Xib file
- you write the drawing code in the
-drawRect:
method.
But if I init
the view using
[[MyView alloc] initWithNibName:@"MyView" bundle:[NSBundle mainBundle]];
-drawRect:
of course doesn't get called. I tried doing the below code in -viewDidLoad
CGRect rect = [[self view] bounds];
CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ref, 2.0);
CGContextSetRGBStrokeColor(ref, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBFillColor(ref, 0, 0, 0, 0);
CGContextAddRect(ref, CGRectMake(1, 1, rect.size.width - 10, rect.size.height - 10));
CGContextStrokePath(ref);
CGContextDrawPath(ref, kCGPathFillStroke);
But nothing get drawn. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为问题在于您将视图及其视图控制器视为可互换的。 例如,没有
-[UIView initWithNibName:bundle:]
方法 - 这是一个 UIViewController 方法。此外,视图实际上并不像绘图的“画布”。 将要求视图自行绘制; 一般来说,它不会从“外部”被吸引进来。
因此:
将 UIViewController 的子类从 MyView 重命名为 MyViewController。
创建一个名为 MyView 的新 UIView 子类。
向新的 MyView 类添加一个
-drawRect:
方法,用于执行所需的绘图。最后,使用 Identity Inspector 将 Interface Builder 中视图控制器视图的自定义类设置为 MyView。
例如,您应该能够将其用于您的
-[MyView drawRect:]
实现:绘图将被剪切到传入的更新矩形。
I think the issue is that you're treating a view and its view controller as interchangeable. For example, there's no
-[UIView initWithNibName:bundle:]
method — that's a UIViewController method.Furthermore, a view isn't really like a "canvas" for drawing. A view will be asked to draw itself; in general, it won't be drawn into from "outside."
So:
Rename your subclass of UIViewController from MyView to MyViewController.
Create a new UIView subclass named MyView.
Add a
-drawRect:
method to your new MyView class that does the drawing you want.Finally, set the Custom Class of your view controller's view in Interface Builder to MyView using the Identity Inspector.
For example, you should be able to use this for your
-[MyView drawRect:]
implementation:The drawing will be clipped to the update rectangle passed in.
以下是三种可能的解决方案,具体取决于您的限制。
addSubView:
方法将其添加到现有视图。Here are three possible solutions, depending on your constraints.
addSubView:
method.在一个简单的子视图中完成所有绘图并将其添加到 XIB 视图(在 viewDidLoad 中)怎么样?
How about doing all the drawing in a simple subview and add it to the XIB view (in viewDidLoad)?