iOS- 11设置导航条(扩展类UIBarButtonItem)

来源:互联网 发布:淘宝卖家给买家的好评 编辑:程序博客网 时间:2024/06/10 04:36

小知识:

1.UIBarButtonItem:描述按钮具体的内容
2.UINavigationItem:设置导航条上的内容(左边,右边,中间)
3.tabBarItem:设置tabBar上的按钮内容(tabBarButton)

左右item

问题:

  • UIButton包装成UIBarButtonItem就会导致点击区域扩大

    点击区域扩大

    应该包装成UIVIew在进行添加

    包装成UIVIew


    效果图
  • 扩展类UIBarButtonItem-代码如下
    UIBarButtonItem+ZYExtension.h

     #import <UIKit/UIKit.h> @interface UIBarButtonItem (ZYExtension) + (instancetype)itemWithNorImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action; @end

    UIBarButtonItem+ZYExtension.m

      #import "UIBarButtonItem+ZYExtension.h"  @implementation UIBarButtonItem (ZYExtension)   + (instancetype)itemWithNorImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action {     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];     [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];     [btn setImage:[UIImage imageNamed:highImage] forSt ate:UIControlStateHighlighted];     [btn sizeToFit];     [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];     UIView *containVew = [[UIView alloc] initWithFrame:btn.bounds];     [containVew addSubview:btn];     return [[UIBarButtonItem alloc]initWithCustomView:containVew];  }

    使用方法:(例如)

      self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithNorImage:@"nav_item_game_icon" highImage:@"nav_item_game_click_icon" target:self action:@selector(game)];

设置导航条标题

  • 只要通过模型设置,都需要通过付文本设置
  • 设置导航条标题
     self.navigationItem.title = @"我的"; NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; attrs[NSFontAttributeName] = [UIFont  boldSystemFontOfSize:20]; [self.navigationController.navigationBar setTitleTextAttributes:attrs];
  • titleVIew
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:  [UIImage imageNamed:@"MainTitle"]];

片段代码:(调用)

 - (void)setupNavBar   {   //   左边Item               self.navigationItem.leftBarButtonItem = [UIBarButtonItem                                                                       itemWithNorImage:@"nav_item_game_icon"                                                                              highImage:@"nav_item_game_click_icon"                                                                                 target:self                                                                                 action:@selector(game)];   //   右边Item                 self.navigationItem.rightBarButtonItem = [UIBarButtonItem                                                               itemWithNorImage:@"navigationButtonRandom"                                                                            highImage:@"navigationButtonRandomClick"                                                                                    target:self                                                                                    action:nil];   //   中间titleView                self.navigationItem.titleView = [[UIImageView alloc]initWithImage:  [UIImage imageNamed:@"MainTitle"]];   }

 - (void)setupNavBar{//   设置UIBarButtonItem *settingItem = [UIBarButtonItem itemWithNorImage:@"mine-setting-icon" highImage:@"mine-setting-icon-click" target:self action:@selector(setting:)];//   夜间模式UIBarButtonItem *nightItem = [UIBarButtonItem itemWithNorImage:@"mine-moon-icon" selImage:@"mine-moon-icon-click" target:self action:@selector(night:)];//   设置self.navigationItem.rightBarButtonItems = @[settingItem,nightItem];//   只要通过模型设置,都需要通过富文本设置//   设置导航条标题 ==>  UINavigationBarNSMutableDictionary *attrs = [NSMutableDictionary dictionary]; attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];[self.navigationController.navigationBar setTitleTextAttributes:attrs]; } #pragma mark- 夜间模式以及设置点击事件- (void)night:(UIButton *)btn{//  选中状态-必须手动btn.selected = !btn.selected;}- (void)setting:(UIButton *)btn{}

原创粉丝点击