如何创建自定义NavigationBar子类并在其中添加多个Item

MKF*_*KFB 5 objective-c uinavigationbar uinavigationcontroller uinavigationitem ios

我想创建UINavigationBar具有多个项目的自定义子类。但是我不知道

我也想获得这个自定义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;
}
Run Code Online (Sandbox Code Playgroud)

导航栏

#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
}
*/
Run Code Online (Sandbox Code Playgroud)

Mag*_*goo 0

要将自定义导航栏放入视图控制器中,您应该将其放在UINavigationController子类中

自定义导航控制器.m

@implementation CustomNavigationController


-(UINavigationBar *)navigationBar
{
     return [[MYCustomNavBar alloc]init];
}
Run Code Online (Sandbox Code Playgroud)

但实际上,如果您只想更改普通导航栏的外观,则不需要这样做。您可以在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];

}
Run Code Online (Sandbox Code Playgroud)

然后在添加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;
}
Run Code Online (Sandbox Code Playgroud)

SomeViewController.m

在 viewController 子类中,您可以像这样访问导航栏

self.navigationController.navigationBar

但是,要将不同的项目添加到导航栏,您可以使用

self.navigationItem.rightBarButtonItemself.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];
Run Code Online (Sandbox Code Playgroud)

或者,如果您不想要定制的容器视图并且对项目之间的统一/动态间距感到满意,您可以使用

self.navigationItem.rightBarButtonItems = @[item1,item2];
Run Code Online (Sandbox Code Playgroud)

感谢@Fawkes 的补充。