`
jsntghf
  • 浏览: 2475982 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

手动调用drawRect方法出现的问题

    博客分类:
  • iOS
阅读更多

UINavigationBar+BackGroundImg.h

 

#import <UIKit/UIKit.h>

@interface UINavigationBar (BackGroundImg)

@end

 

UINavigationBar+BackGroundImg.m

 

#import "UINavigationBar+BackGroundImg.h"

@implementation UINavigationBar (BackGroundImg)

- (void)drawRect:(CGRect)rect {
    UIImage *img = [UIImage imageNamed:@"nav_bg.png"];
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

 

原来使用的[self.navigationController.navigationBar drawRect:CGRectZero];运行一切正常,但控制台输出报错信息:

 

<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextSetBlendMode: invalid context 0x0
<Error>: CGContextSetAlpha: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextScaleCTM: invalid context 0x0
<Error>: CGContextDrawImage: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0

 

这是由于直接手动调用drawRect方法导致的。

 

将调用方式改成以下形式:

 

[self.navigationController.navigationBar setNeedsDisplay];

 

通过调用setNeedsDisplay方法,会自动调用到drawRect方法,这样问题就解决了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics