4

我想創建UINavigationBar具有多項目的自定義子類。但我不知道這件事。如何創建自定義NavigationBar子類並在其中添加多個項目

也我想要得到這個自定義NavigationBarUIViewControllers並改變它。

請指導我: 1-如何創建自定義NavigationBar子類的項目。 2-如何獲取此自定義導航並更改其中的項目。

這是我的代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]]; 
    ViewController *rootView = [[ViewController alloc]init]; 

    UINavigationController *navi = [[UINavigationController alloc] initWithNavigationBarClass:[NavBar class] toolbarClass:nil]; 
    navi.viewControllers = @[rootView]; 
    self.window.rootViewController = navi; 

    [window makeKeyAndVisible]; 
    return YES; 
} 

NavBar.m

#import "NavBar.h" 

@implementation NavBar 
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
    [self setup]; 
    } 
    return self; 
} 

- (id)initWithCoder:(NSCoder *)aDecoder { 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
    [self setup]; 
    } 
    return self; 
} 

- (void)setup { 
    [self setupBackground]; 
} 

- (void)setupBackground { 
    self.backgroundColor = [UIColor clearColor]; 
    self.tintColor = [UIColor blueColor]; 

    // make navigation bar overlap the content 
    self.translucent = YES; 
    self.opaque = NO; 

    // remove the default background image by replacing it with a clear image 
    [self setBackgroundImage:[self.class maskedImage] forBarMetrics:UIBarMetricsDefault]; 

    // remove defualt bottom shadow 
    [self setShadowImage: [UIImage new]]; 
} 

+ (UIImage *)maskedImage { 
    const CGFloat colorMask[6] = {222, 255, 222, 255, 222, 255}; 
    UIImage *img = [UIImage imageNamed:@"nav-white-pixel-bg.jpg"]; 
    return [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)]; 
} 
/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect { 
    // Drawing code 
} 
*/ 

回答

0

爲了讓您的自定義導航欄到您的viewController,你應該有它的子類UINavigationController

CustomNavigationController.m

@implementation CustomNavigationController 


-(UINavigationBar *)navigationBar 
{ 
    return [[MYCustomNavBar alloc]init]; 
} 

雖然其實你並不需要做,如果你只是想改變一個正常的導航欄的外觀..你可以在自定義的viewDidLoad做到這一點navigationController

-(UIStatusBarStyle)preferredStatusBarStyle 
{ 
    return UIStatusBarStyleLightContent; 
} 

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.navigationBar.translucent = NO; 

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:16.0f],NSFontAttributeName,[UIColor redColor], NSForegroundColorAttributeName,nil]; 

    [[UINavigationBar appearance] setTitleTextAttributes:dictionary]; 

} 

然後添加你的viewController時,你需要用它導航控制器

AppDelegate.m

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

    // other launching code 

    SomeViewController *viewController = [[SomeViewController alloc]init]; 
    CustomNavigationController *navigationController = [[CustomNavigationController alloc]initWithRootViewController:viewController]; 

    self.window.rootViewController = navigationController; 
    [self.window makeKeyAndVisible]; 

    return YES; 
} 

SomeViewController.m

在你的viewController子類可以訪問導航欄這樣

self.navigationController.navigationBar

但是不同的項目添加到您的導航欄,您可以使用

self.navigationItem.rightBarButtonItem and self.navigationItem.leftBarButtonItem

爲了有一個以上的鍵/項目無論是在左邊或右邊部分,您可以創建自定義視圖,並且將其添加爲欄按鈕項...

UIView *containerView = [UIView alloc]initWithFrame: // etc... 

// create your items` 

[containerView addSubview:item1]; 
[containerView addSubview:item2]; // etc... 

// then add the custom view as the navigation item.. 

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:containerView]; 

或者,如果你不想一定製容器視圖,並與你可以使用的項目之間的統一/動態間距感到滿意

self.navigationItem.rightBarButtonItems = @[item1,item2]; 

謝謝@Fawkes的補充。

+1

什麼? – Fawkes

+0

也是如此,雖然我經常發現這對自定義間距等更好...這並沒有被定義爲一個要求,但我會將它添加到我的答案。乾杯。 – Magoo

0

這聽起來像你在找什麼是UISegmentedControl

您可以將其添加到故事板中的UINavigationBarprogrammatically

肯定會給上UISegmentedControl的本文檔的外觀以及如何使用直接self.navigationItem.rightBarButtonItems沒有容器視圖

相關問題