5

I want create UINavigationBar custom subclass with multi items. but I don't know about this.

also I want to get this custom NavigationBar in UIViewControllers and change it.

please guide me : 1- how to create custom NavigationBar subclass with items. 2- how to get this custom Nav and change items in it.

this is my code:

- (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
}
*/
MKFB
  • 185
  • 1
  • 10

2 Answers2

1

It sounds like what you are looking for is a UISegmentedControl.

You can either add this to a UINavigationBar in a storyboard or programmatically.

Definitely give the documentation on UISegmentedControl's a look as well

Community
  • 1
  • 1
sharpnado
  • 31
  • 4
0

To get your custom NavigationBar into your viewController, you should have it in a UINavigationController subclass

CustomNavigationController.m

@implementation CustomNavigationController


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

though actually you don't need to do this if you'd just like to change the appearance of a normal navigationbar.. you can do this in the viewDidLoad of your custom 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];

}

Then when adding your viewController, you need to wrap it in the navigation controller

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

In your viewController subclass you can access the navigation bar like this

self.navigationController.navigationBar

However to add different items to your navigation bar, you can use

self.navigationItem.rightBarButtonItem and self.navigationItem.leftBarButtonItem

To have more than one button / item in either the left or right section, you can create a custom view, and add that as your bar button item...

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];

Alternatively if you don't want a bespoke container view and are happy with uniform / dynamic spacing between your items you can use

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

Thanks @Fawkes for the addition.

Magoo
  • 2,312
  • 1
  • 19
  • 38
  • 1
    what about using directly self.navigationItem.rightBarButtonItems without container view? – Fawkes May 18 '15 at 13:48
  • also true, though I've often found this better for custom spacing etc... this wasn't defined as a requirement though so i'll add it to my answer. cheers. – Magoo May 18 '15 at 13:58