How to use custom UINavigationBar(如何使用自定义 UINavigationBar)
问题描述
我有 UINavigationBar 的子类.
@interface MyNavigationBar : UINavigationBar
做了一些更改,现在希望我的应用程序 NavigationController 使用它:
Made some changes and now want that my application NavigationController would use it:
_navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[_window addSubview:[_navigationController view]];
[self.window makeKeyAndVisible];
我希望 _navigationController 有 MyNavigationBar
如何做到这一点?
谢谢.
推荐答案
你必须创建一个带有 UINavaigationController 的 xib.然后,您可以在 Interface Builder 中选择 navigationBar 并将类更改为 UINavigationBar 的子类.
You have to create a xib with a UINavaigationController in it. You can then select the navigationBar in Interface Builder and change the class to your subclass of UINavigationBar.
然后为了更容易实例化,我向 `UINavigationController 添加了一个类别,例如:
Then to make this a little easier to instantiate I add a category to `UINavigationController like:
@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
在使用它的类的顶部,确保在我的情况下导入类别,它看起来像
At the top of the class that uses it makes sure to import the category in my case it looks like
#import "UINavigationController+DSCNavigationController"
然后使用它看起来像
MyViewController *myViewController = [[MyViewController alloc] init];
UINavigationController *navigationController = [UINavigationController dsc_navigationControllerWithRootViewController:myViewController];
这篇关于如何使用自定义 UINavigationBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用自定义 UINavigationBar
基础教程推荐
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 我的 UIImageView 的任务 2022-01-01
