UINavigationBar's drawRect is not called in iOS 5.0

后端 未结 7 1691
清歌不尽
清歌不尽 2020-11-28 07:59

I\'ve overrided(placed in category, or swizzled) UINavigationBar\'s drawRect to show custom background. In iOS 5 it\'s not working. What should I do?

相关标签:
7条回答
  • 2020-11-28 08:27

    There's some possible solutions:

    Quickest fix For laziest of us :

    @interface MyNavigationBar : UINavigationBar
    
    @end
    
    @implementation MyNavigationBar
    
    - (void)drawRect:(CGRect)rect {
    
    }
    
    @end
    
    @implementation UINavigationBar (BecauseIMLazyHacks)
    /*
     Another Ugly hack for iOS 5.0 support
    */
    + (Class)class {
      return NSClassFromString(@"MyNavigationBar");
    }
    
    -(void)drawRect:(CGRect)rect {
    
      CGContextRef context = UIGraphicsGetCurrentContext();
      CGContextTranslateCTM(context, 0, self.frame.size.height);
      CGContextScaleCTM(context, 1.0, -1.0);
    
      CGContextDrawImage(context, CGRectMake(0, 0, 
      self.frame.size.width, self.frame.size.height), barBackground.CGImage);      
    
    }
    @end
    

    Again. It works, but You shouldn't do it like this.

    Another way, as suggested in WWDC'11 is to override UINavigationBar (Create MyNavigationBar) and initialize UINavigationController from xib like here :

    http://www.iosdevnotes.com/2011/09/custom-uinavigationbars-techniques/

    And finally, use logic flow switch for iOS5.0 and iOS5.0- Use new API where it's possible.

    Categories is wrong path, Swizzling is wrong path. (They're just whispering in your ears:"Give yourself to the Dark Side. It is the only way you can save your apps.")

    0 讨论(0)
提交回复
热议问题