2012-04-04 36 views
0

我有UINavigationBar的子類。如何使用自定義UINavigationBar

@interface MyNavigationBar : UINavigationBar 

做了一些改動,現在想的是我的應用程序NavigationController會使用它:

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

我想那_ navigationController將有MyNavigationBar

怎麼可以這樣做?

謝謝。

回答

4

你必須創建一個有一個UINavaigationController一個廈門國際銀行。然後,您可以在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]; 
+0

你能告訴我它在AppDelegate中的外觀嗎? – Streetboy 2012-04-04 09:12:24

+0

工作。但是這不起作用[self.navigationController pushViewController:sendMsg animated:YES]; – Streetboy 2012-04-04 09:44:14

+1

你是從哪個班打來的?檢查'NSLog(@「%@」,self.navigationController);'是否爲空 – 2012-04-04 09:50:27

1

的UINavigationController有一個只讀屬性

@property(nonatomic, readonly) UINavigationBar *navigationBar 

,因爲它是隻讀的,你必須繼承UINavigationBar的和覆蓋此屬性或使其讀write.E.g。 :

MyNaviagtionBar *myBar = [[MyNavigationBar alloc] init]; 
_navigationController.navigationBar = mybar; 

或者子類:

MyNavigationController.h

@class MyNavigationBar; 

@interface MyNavigationController : UINavigationController 
@property(nonatomic, strong) MyNavigationBar *navigationBar; 
@end 

MyNavigationController.m

@implementation MyNavigationController 
@synthesize navigationBar = _navigationBar; 
@end 

,然後改變

_navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 

_navigationController = [[MyNavigationController alloc] initWithRootViewController:self.viewController]; 
+0

我添加了這個接口: 但是,當我嘗試做你表明我得到這樣的: 分配新建分配FY閱讀 – Streetboy 2012-04-04 08:11:31

+0

加入繼承 – Pfitz 2012-04-04 08:19:25

+0

我不能做到這一點火一個錯誤的唯一屬性@synthesize的導航欄= _navigationBar ; 屬性'navigationBar'嘗試使用在超類'UINavigationController'中聲明的ivar'_navigationBar' – Streetboy 2012-04-04 08:38:44

相關問題