zoukankan      html  css  js  c++  java
  • iOS Navigation自定义设置Item

    1. 自定义NavigationController:

    @interface CustomNavigationController : UINavigationController

    2. 重写Push方法, 拦截Push进来的控制器:

    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
    {
         // 判断Push进来的控制器是否根控制器
         if (self.childViewControllers.count > 0)
         {
              // Push时隐藏TabBarBottom(应用层级结构为TabBarController -> NavigationController时)
              viewController.hidesBottomBarWhenPushed = YES;
           
              // 添加左边Item到navigationItem
              viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self image:@“leftBarButtonImage" highlightedImage:@"leftBarButtonImage_highlighted" action:@selector(clickLeftItem)];             
    
              // 添加左边Item到navigationItem
              viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self image:@“rightBarButtonImage" highlightedImage:@“rightBarButtonImage_highlighted" action:@selector(clickRightItem)];             
         }
       
         [super pushViewController:viewController animated:YES];  
    }

    Item点击事件:

    - (void)clickLeftItem
    {
         // Method
         [self popViewControllerAnimated:YES];
    }
    - (void)clickLeftItem
    {
         // Method
    }

    3. 通过Category,自定义BarButtonItem创建方法:

    + (UIBarButtonItem *)itemWithTarget:(id)target image:(NSString *)imageName highlightedImage:(NSString *)highlightedImageName action:(SEL)action
    {
        // 创建按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        // 设置按钮图片
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:highlightedImageName] forState:UIControlStateHighlighted];
        // 设置按钮尺寸
        button.size = button.currentImage.size; // 避免图片拉伸
        // 设置按钮监听事件
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
       
        // 返回Item
        return [[UIBarButtonItem alloc] initWithCustomView:button];
    }
  • 相关阅读:
    js 原生ajax实现
    layer 查看图片
    c# 操作XML
    C# 扩展方法
    c# 依赖注入
    visual studio 快捷键
    HIS系统结算后,没有更新单据状态为“已结算”
    网络流四·最小路径覆盖 HihoCoder
    飞行员配对(二分图最大匹配) 51Nod
    开心的小Q 51Nod
  • 原文地址:https://www.cnblogs.com/happyplane/p/4704320.html
Copyright © 2011-2022 走看看