iOS8问题 - 未调用UIView drawRect [英] iOS8 issue - UIView drawRect is not called
问题描述
我有一个非常简单的 UIView
,它只绘制一个三角形。它实现了一个绘制图形的 UIView drawRect
方法。它在iOS7上工作正常,但是当我在iOS8上运行相同的代码时,它甚至都没有被调用。任何想法在iOS8上发生了什么变化?简而言之我的代码:
I have a very simple UIView
, that is only drawing a triangle. It implements a UIView drawRect
method that is drawing a figure. It is working fine on iOS7, but when I run the same code on iOS8 it is not even called. Any ideas what has changed on iOS8? My code in nutshell:
@interface MyView : UIView
@end
@implementation MyView
- (instancetype)init {
self = [super init];
if (self) {
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)drawRect:(CGRect)rect {
// draw things
}
@end
myView = [MyView new];
[self addSubview:myView];
更新
我的视图层次结构: UIViewController查看 - >自定义UILabel-> MyView
这是一个错误消息气泡,我的视图是喙指向某事。
It is an error message bubble, and my view is a beak pointing to something.
我使用了一个新的UI调试工具,但在那里看不见。调用Init方法, drawRect
不是。 iOS8中的内容必定已更改,因为iOS7正在调用 drawRect
。
I have used a new UI debugging tool, but it is not visible there. Init method is called, drawRect
is not. Something must have changed in iOS8, because iOS7 is calling drawRect
.
更新2
当然我正在使用约束(以及 Masonry
pod),这就是我没有指定框架的原因。
Update 2
Of course I'm using constraints (and Masonry
pod), and this is why I did not specify the frame.
[myView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(make.superview.mas_bottom);
make.centerX.equalTo(make.superview);
make.width.equalTo(@10.0f);
make.height.equalTo(@5.0f);
}];
我还尝试添加 [myView setNeedsDisplay]
在各个地方,但没有帮助。
I have also tried adding [myView setNeedsDisplay]
in various places, but it didn't help.
推荐答案
问题终于解决了。我不确定究竟是什么导致了这个问题( Masonry
[约束框架]或iOS8更改为 UILabel
),但是解决方案是更改视图层次结构。我创建了另一个 UIView
,它包含 UILabel
和我的 UIView
(绘制的喙)而不是将 UIView
添加到 UILabel
作为子视图。现在 drawRect
方法在iOS7和iOS8上都被调用。
Problem finally solved. I'm not sure what exactly caused the issue (Masonry
[constraints framework] or iOS8 changes to UILabel
), but the solution was to change the view hierarchy. I created another UIView
that contains both UILabel
and my UIView
(drawn beak) instead of adding the UIView
to UILabel
as subview. Now drawRect
method is called both on iOS7 and iOS8.
以前的层次结构:
UIViewController查看 - >自定义UILabel-> MyView
新层次结构:
UIViewController View-> Container UIView-> Custom UILabel& MyView
总之,如果你的 drawRect
方法没有在iOS8上调用,并且您要将 UIView
添加到 UILabel
作为子视图,尝试使用一些包含<$ c $的容器c> UIView 和 UILabel
。
To sum up, if your drawRect
method is not called on iOS8, and you are adding some UIView
to UILabel
as subview, try to use some container which will contain both UIView
and UILabel
.
这篇关于iOS8问题 - 未调用UIView drawRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!