我有UINavigationBar的子类。

@interface MyNavigationBar : UINavigationBar


进行了一些更改,现在希望我的应用程序NavigationController将使用它:

 _navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
 [_window addSubview:[_navigationController view]];
[self.window makeKeyAndVisible];


我希望_navigationController具有MyNavigationBar

如何做到这一点?

谢谢。

最佳答案

您必须使用xcc创建一个xib。然后,您可以在Interface Builder中选择UINavaigationController,并将该类更改为navigationBar的子类。



然后,为了使实例化起来更容易一些,我向UINavigationController添加了一个类别,例如:

@interface UINavigationController (DSCNavigationController)

+ (UINavigationController *)dsc_navigationControllerWithRootViewController:(UIViewController *)rootViewController;

@end

@implementation UINavigationController (DSCNavigationController)

+ (UINavigationController *)dsc_navigationControllerWithRootViewController:(UIViewController *)rootViewController;
{
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DSCNavigationController" owner:nil options:nil];

    NSAssert(1 == [topLevelObjects count], @"DSCNavigationController should have one top level object");

    UINavigationController *navigationController = [topLevelObjects objectAtIndex:0];

    NSAssert([navigationController isKindOfClass:[UINavigationController class]], @"Should have a UINavigationController");

    [navigationController pushViewController:rootViewController animated:NO];

    return navigationController;
}

@end


在使用它的类的顶部,请确保在我的情况下导入类别,例如

#import "UINavigationController+DSCNavigationController"


然后使用它看起来像

MyViewController *myViewController = [[MyViewController  alloc] init];
UINavigationController *navigationController = [UINavigationController dsc_navigationControllerWithRootViewController:myViewController];

10-07 19:51
查看更多