Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create custom NavigationBar subclass and add multi Item in it

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
}
*/
like image 553
MKFB Avatar asked Nov 10 '22 13:11

MKFB


1 Answers

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

like image 165
sharpnado Avatar answered Nov 14 '22 21:11

sharpnado