How to overide UIToolbar method with later version of iOS 4.2

651 views Asked by At

This code works fine with iPad Simulator 4.2, but not with later version of iOS4.3 or after that.I am not able to Override the UIToolbar class methods.

@implementation UIToolbar (CustomImage)
- (void)drawRect:(CGRect)rect 
{
    UIImage *image = [[UIImage imageNamed:@"ToolBar.png"] retain];
    [image drawInRect:rect];
    [image release];    
}
 //return 'best' size to fit given size. does not actually resize view. Default is return existing view size
- (CGSize)sizeThatFits:(CGSize)size {
    CGSize result = [super sizeThatFits:size];
    result.height = 80;
    return result;
};  

What would be the alternate solution for this ?Please guide me. In later version ..- (void)drawRect:(CGRect)rect is never called .

Running with iPad Simulator 4.2 code works fine but with iPad Simulator 4.3 drawRect in never called.

Below is the Screenshot of Toolbar:

enter image description here

enter image description here

2

There are 2 answers

2
Michael Frederick On

What about something like this?

@implementation UIToolbar (UIToolbarCategory)
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    if([self isMemberOfClass: [UIToolbar class]]){
        [super drawRect:rect];
        UIImage *image = [UIImage imageNamed:@"bar_gradient.png"];
        [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    }
}
@end
0
Ma7dy On

you are implementing it as a category, you need to subclass UIToolBar based on the iOS5 change log

In the iOS 5 beta, the UINavigationBar, UIToolbar, and UITabBar implementations have changed so that the drawRect: method is not called on instances of these classes unless it is implemented in a subclass.

Apps that have re-implemented drawRect: in a category on any of these classes will find that the drawRect: method isn’t called.

UIKit does link-checking to keep the method from being called in apps linked before iOS 5 but does not support this design on iOS 5 or later. Apps can either:

  • Use the customization API for bars that in iOS 5 and later, which is the preferred way.
  • Subclass UINavigationBar (or the other bar classes) and override drawRect: in the subclass.