0
votes

Currently making a universal application in which, I m using drawRect method for displaying an image on navigationBar for iPhone app, but I don't want to use this method for iPad.

I have created a category for changing UINavigationBar. In which I did the following.

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)   
    { 
        // iPad mode ... dont load anything 
    }
    else 
    {
        // iphone mode .... use an image 
    }

After this its displaying a black empty space instead of navigation bar for iPad. For iPhone it works fine.

I tried using self.navigationController.navigationBar.barStyle = UIBarStyleDefault; in my both Matser & Detail views in viewDidLoad , but still it displays black empty space.

How can I avoid drawRect method call when device is iPad.

Thanks

2

2 Answers

1
votes

If you were subclassing UINavigationBar, you should just use the right navigation bar class on each platform. In other words, the interface idiom check should affect creation of the navigation bar, rather than its drawRect: implementation.

Since you are using a category to replace the drawRect: implementation (which you shouldn't do), I think your only choice is dynamic method swizzling. That is, you replace drawRect: only on iPhone/iPod Touch.

Consider subclassing UINavigationBar as it is easier, safer and cleaner. That will help you avoid compatibility headaches.

EDIT: to provide an example, assume that you have a nib file containing only an empty navigation controller with a custom UINavigationBar subclass. To do that select the navigation bar in the xib editor and change its class in the inspector.

UINavigationController *navigationController = nil;

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    navigationController = [[UINavigationController alloc] init];
else {
    UINib *nib = [UINib nibWithNibName: @"MyNavigationController" bundle: nil];
    navigationController = [[[nib instantiateWithOwner: nil options: nil] objectAtIndex: 0] retain];
}

When run on an iPhone or an iPod Touch, this code will create a navigation controller from the nib file.

0
votes

If you call [super drawRect] at the beginning of the method, it will call the default implementation which will draw the UINavigationBar normally.

The reason that you should not do a custom subclass is that UINavigationController instantiates it's UINavigationBar directly and you cannot tell it to use your subclass.