gpt4 book ai didi

objective-c - 如何使用自定义 UINavigationBar

转载 作者:行者123 更新时间:2023-11-28 18:42:00 25 4
gpt4 key购买 nike

我有 UINavigationBar 的子类。

@interface MyNavigationBar : UINavigationBar

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

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

我希望 _navigationControllerMyNavigationBar

如何做到这一点?

谢谢。

最佳答案

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

enter image description here

然后为了让它更容易实例化,我向`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];

关于objective-c - 如何使用自定义 UINavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10006011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com