2014-04-01 75 views
0

我有一個問題,我目前找不到解決方案。在我的應用程序中,我在NavigationBar中有多個按鈕,這些按鈕在整個應用程序中都是必需的,而不是在每個視圖控制器中創建按鈕,我想要製作UINavigationBar或UINavigationController的子類(我不知道哪一個)。因此,無論何時用戶在視圖之間移動,導航欄始終包含這些按鈕。至今我一直在搜尋這件事,但找不到任何值得的東西。 請提前告訴我一個這樣做的方法。ios6和ios7中的自定義導航欄

回答

0
#import "CustomNavBar.h" 

@implementation CustomNavBar 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     self.tintColor=[UIColor greenColor]; 
    } 
    return self; 
} 
- (void)drawRect:(CGRect)rect { 
    UIImage *image = [UIImage imageNamed:@"Custom-Nav-Bar-BG.png"]; 
    [image drawInRect:CGRectMake(0, 0, 40, self.frame.size.height)]; 

    UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd]; 
    [btn drawRect:CGRectMake(42, 0, 40, self.frame.size.height)]; 

    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeContactAdd]; 
    [btn2 drawRect:CGRectMake(82, 0, 40, self.frame.size.height)]; 
} 


@end 
0

你也可以繼承標準UINavigationBar實現這一

@interface CustomNavigationBar : UINavigationBar 

- (id)initWithFrame:(CGRect)frame; 

@end 

@implementation CustomNavigationBar 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

     UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd]; 
     [btn addTarget:self action:@selector(<#selector#>) forControlEvents:<#(UIControlEvents)#>] 
     [self addSubview:btn]; 

     ... 
     ... 


    } 
    return self; 
} 
- (void)drawRect:(CGRect)rect { 

    [[UIColor redColor] setFill]; 
    UIRectFill(rect); 
} 

@end 

要使用這是StoryBoardxib,簡單地改變標準類名CustomNavigationBar

OR

如果你想以編程方式

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]]; 

    UINavigationController *navVC = [[UINavigationController alloc] initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil]; 

    UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 

    ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:@"VC"]; 


    [navVC setViewControllers:[NSArray arrayWithObject:VC1] animated:NO]; 
    [self. window setRootViewController:navVC]; 

    [self. window makeKeyAndVisible]; 

    return YES; 
}