您的当前位置:首页自定义Navigation Bar 返回按钮

自定义Navigation Bar 返回按钮

2024-12-14 来源:哗拓教育
  • 方法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];
}


显示全文