drawRect不想画任何东西 [英] drawRect Doesn't want to draw anything
问题描述
我在故事板上有一个ViewController.我已经使用界面生成器在屏幕底部设置了一个工具栏.我已经将自定义视图设置为覆盖drawRect的视图.但是,对于我一生来说,我无法任何事情出现在该drawRect调用的屏幕上. drawRect本身就很好,但是屏幕上什么都没显示.
I have a ViewController on storyboard. I have used the interface builder to set a toolbar at the bottom of the screen. I have set the custom view to a view overrides drawRect. However, for the life of me, I cannot get anything ever to show up on screen called from that drawRect. drawRect itself is called just fine, but nothing shows up on screen.
此外,我还有一个ViewController及其方法,该方法使用AVCaptureSession将其背景从摄像机输入切换为实时取景.我曾怀疑这可能是导致错误的原因,但是在删除所有AVCaptureSession的引用之后,我仍然无法使它正常工作.
Also, I have this ViewController with a method that uses AVCaptureSession to toggle its background to a live view from camera input. I had suspected that this might have been the cause for error, but after removing all references of AVCaptureSession, I still cannot get this to work.
抱歉,我的写作和/或缺乏逻辑,我现在没有任何睡眠.
Sorry for my writing and/or lack of logic, I don't have any sleep right now.
这是一个不起作用的小代码示例.里面的每个方法都被调用,但是什么也没显示.
Here is a small example of code that won't work. Every method inside gets called, but nothing is to show.
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
// Draw them with a 2.0 stroke width so they are a bit more visible.
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, 0,0); //start at this point
CGContextAddLineToPoint(context, 100, 100); //draw to this point
// and now draw the Path!
CGContextStrokePath(context);
}
推荐答案
UIView
的文档说,如果您要覆盖UIView
,请不要调用[super drawRect]
.删除该电话!众所周知,它会引起奇怪的行为.
The documentation for UIView
says to not call [super drawRect]
if you're overriding UIView
. Remove that call! It's been known to cause strange behaviour.
根据文档:
如果直接子类化UIView,则此方法的实现 不需要调用超级.但是,如果您将一个子类 不同的视图类,您应该在您的某个时候调用super 实施.
If you subclass UIView directly, your implementation of this method does not need to call super. However, if you are subclassing a different view class, you should call super at some point in your implementation.
这篇关于drawRect不想画任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!