自定义Navigation Bar 返回按钮

作者: Show_Perry | 来源:发表于2017-04-20 15:48 被阅读129次

    简单的技巧往往容易忽略,今天Check小伙伴的代码时发现自定义返回按钮弄得很复杂,虽然效果达到了但是并不是理想的方法,今天介绍三种自定义的方法。
    iOS 11 后会有所改变需要重新适配:具体请看 iOS 11 后如何自定义返回按钮

    • 方法1 当你只需要自定义返回按钮的标题,不需要自定义返回按钮的事件时。

    定义一个基于UIViewControllercategory:添加两个方法

    @interface UIViewController (navigationItemCustom)
    
    /**
     自定义导航栏返回按钮标题
    
     @param title 标题
     */
    - (void)customBackButtonWithTitle:(NSString *)title;
    
    
    - (void)customBackButtonWithTitle:(NSString *)title
    {
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:title style:UIBarButtonItemStylePlain target:nil action:nil];
        [[self navigationItem]setBackBarButtonItem:backButton];
    }
    
    

    这里为何targetaction为空呢?因为返回按钮的事件这里自定义了也不会调用!自己可以去试试!

    注意

    • 这里自定义需要在push前调用例如:
    - (IBAction)pushAction:(id)sender
    {
        ViewController *vc = [[ViewController alloc]init];
        [self customBackButtonWithTitle:@"hello"];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    
    
    • 方法2 当你需要自定义返回按钮图片,并且需要自定义返回按钮的事件时。
    /**
      自定义导航栏返回按钮图片
    
     @param imageName 图片名称
     @param action 按钮点击事件
     */
    - (void)customBackButtonWithImageName:(NSString *)imageName action:(SEL)action;
    
    
    - (void)customBackButtonWithImageName:(NSString *)imageName action:(SEL)action
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        [button sizeToFit];
        UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        UIBarButtonItem *spacebutton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        spacebutton.width = -20;//这个数值可以调整
        self.navigationItem.leftBarButtonItems = @[spacebutton,leftBarItem];
        
    }
    
    
    • 方法3 既需要自定义返回按钮的图标又需要自定义返回按钮的标题。
    /**
     自定义导航栏放回按钮View
    
     @param view 自定义的View
     @param action 点击事件
     */
    - (void)customBackButtonWithView:(UIView *)view action:(SEL)action;
    
    
    
    /**
     自定义返回按钮的图标和标题
    
     @param imgName 图片名称
     @param title 标题
     @param action 点击事件
     */
    - (void)customBackButtonWithImageName:(NSString *)imgName withTitle:(NSString *)title action:(SEL)action;
    
    
    - (void)customBackButtonWithView:(UIView *)view action:(SEL)action
    {
        view.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:action];
        tap.numberOfTouchesRequired = 1;
        tap.numberOfTapsRequired = 1;
        [view addGestureRecognizer:tap];
        
        UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:view];
        UIBarButtonItem *spacebutton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        spacebutton.width = -10;//这个数值可以调整
        self.navigationItem.leftBarButtonItems = @[spacebutton,leftBarItem];
    }
    
    
    - (void)customBackButtonWithImageName:(NSString *)imgName withTitle:(NSString *)title action:(SEL)action
    {
        UIFont *font = [UIFont systemFontOfSize:16.0];
        CGFloat maxHeight = 40.0;
        CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width/3.0 - maxHeight;
        CGRect stringRect = [title boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, maxHeight) options:(NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin) attributes:@{ NSFontAttributeName : font} context:nil];
        if (stringRect.size.width>maxWidth) {
            stringRect.size.width = maxWidth;
        }
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, stringRect.size.width + maxHeight, maxHeight)];
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, maxHeight, maxHeight)];
        imgView.image = [UIImage imageNamed:imgName];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [view addSubview:imgView];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(maxHeight, 0, stringRect.size.width, maxHeight)];
        titleLabel.font = font;
        titleLabel.text = title;
        [view addSubview:titleLabel];
        [self customBackButtonWithView:view action:action];
    }
    
    
    

    UIBarButtonSystemItemFixedSpace Blank space to add between other items. Only the width property is used when this value is set. 它是一个空白的空间,是为了调整items之间的间距。这里设置它的宽度为负值,是因为第二种方法自定义返回按钮位置会靠右!

    相关文章

      网友评论

        本文标题:自定义Navigation Bar 返回按钮

        本文链接:https://www.haomeiwen.com/subject/kqjlzttx.html