iOS8 issue - UIView drawRect is not called

心不动则不痛 提交于 2019-12-05 12:00:19

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.

Previous hierarchy:
UIViewController View->Custom UILabel->MyView

New hierarchy:
UIViewController View->Container UIView->Custom UILabel & MyView

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.

make sure that myView's superview property clipToBounds = NO, and that myView rect is on screen

dgangsta

There is an important detail missing in your approach. You need to specify a frame that determines the origin and size of your view. This can be done with the initWithRect method of UIView, or you can set the frame after allocation/initialization. Since you have a custom init method already I would do the following:

myView = [MyView new];
myView.frame = CGRectMake(0.0,0.0,100.0,100.0);
[self addSubview:myView];

This will draw your custom view with an origin point of (0.0,0.0) and a width and height of 100.0.

Furthermore, adding a call to setNeedsDisplay should force the drawRect method to be called if you are still having trouble:

[myView setNeedsDisplay];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!