Custom navigation bar for the “More” View Controll

2019-09-18 22:33发布

With the code below (for lower versions of iOS) I am subclassing UINavigationBar and applying to each navigation bar (of each navigation controller) in my UITabBarController.

@implementation CustomNavigationBar

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

@end

The code works fine for the view controllers that are visibly accessible, but when pressing the "More" tab to access the other view controllers, the custom image does not appear any more. Have I missed something?

3条回答
三岁会撩人
2楼-- · 2019-09-18 23:16

The UITabBarController will create the UINavigationController for the More item, so its UINavigationBar won't be an instance of your class, but a UINavigationBar instead.

You can look at the iOS 5 appearance API to change the look and feel with it.

查看更多
在下西门庆
3楼-- · 2019-09-18 23:16

You can use the moreViewController property of your tabBarController to get the more Navigation Controller (I used this on iOS7 app)

    UINavigationController *moreViewController = tabController.moreNavigationController;
    if(moreViewController)
    {
        [moreViewController.navigationBar setBarTintColor: [UIColor yellowColor]];
        [moreViewController.navigationBar setTintColor: [UIColor whiteColor]];
    }
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-09-18 23:23

The way I customise the More View Controller is to ensure that you don't get the default more controller from the UITabBarController itself - which is what sounds like you are experiencing.

1 Create your own More view controller. It will have its own custom icon

//MyMoreViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"More", @"More");
        self.tabBarItem.image = [UIImage imageNamed:@"MyCustomMoreImage"];
    }
    return self;
}

and then

2 When you initialise your UITabBarController, ensure you send five exactly view controllers to the initialiser AND that your custom more view controller is the root view controller of the 5th item - i.e. commonly you would use a navigation controller with your more view controller being set to it's rootViewController.

查看更多
登录 后发表回答