删除iOS7中的后退箭头

时间:2013-12-12 04:36:53

标签: ios ios7 uinavigationcontroller

enter image description here

我想要合并一个自定义后退按钮 - 我可以使用

获得上述结果
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back-btn"] style:UIBarButtonItemStylePlain target:nil action:nil];

但是如何删除原生蓝色按钮?

8 个答案:

答案 0 :(得分:13)

使用以下代码隐藏后退箭头:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back-btn"]
                                                                         style:UIBarButtonItemStylePlain
                                                                        target:nil
                                                                        action:nil];

if ([UINavigationBar instancesRespondToSelector:@selector(setBackIndicatorImage:)]) {
    [[UINavigationBar appearance] setBackIndicatorImage:[[UIImage alloc] init]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[[UIImage alloc] init]];
}

答案 1 :(得分:10)

在iOS7 +中,您可以使用navigationBar的backIndicatorImagebackIndicatorTransitionMaskImage来获得所需的自定义箭头。下面的Swift代码:

self.navigationController?.navigationBar.backIndicatorImage = UIImage(named:"button-backArrow18x15")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named:"button-backArrowMask18x15")

如果你想隐藏"返回"您可以将上一个视图的title设置为单个空格" "或使用没有标题的自定义UIBarButtonItem

答案 2 :(得分:4)

您可以隐藏后退按钮

 self.navigationItem.hidesBackButton=YES;

要创建自定义leftbarbuttonitem,您可以使用:

UIButton* someButton = [UIButton buttonWithType:UIButtonTypeCustom];
[someButton setFrame:frame];
[someButton setBackgroundImage:@"youArroImageName" forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem* someBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];
[self.navigationItem setLeftBarButtonItem:someBarButtonItem];

答案 3 :(得分:3)

检查一下:

    UIImage *faceImage = [UIImage imageNamed:@"back_arrow.png"];
    UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
    face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );
    [face addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
    [face setImage:faceImage forState:UIControlStateNormal];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
    self.navigationItem.leftBarButtonItem = backButton;
    [self.navigationItem setHidesBackButton:YES animated:YES];
    [self.navigationItem setBackBarButtonItem:nil];


  -(IBAction)handleBack:(id)sender
  {
     //    [self dismissViewControllerAnimated:YES completion:nil];
     [self.navigationController popViewControllerAnimated:YES];
  }

答案 4 :(得分:3)

感谢所有的回答者 - 找到了一种更简单的方法......

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back-btn"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonAction:)];
self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

[self.navigationItem setHidesBackButton:YES animated:NO];
[self.navigationItem setBackBarButtonItem:nil];

-(void) backButtonAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

答案 5 :(得分:0)

为什么不能尝试使用自定义UIButton:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 40.0f)];
    [btn addTarget:self action:@selector(buttonEN:) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:[UIImage imageNamed:@"back-btn"] forState:UIControlStateNormal];
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.backBarButtonItem = backBtn;

答案 6 :(得分:0)

我刚遇到了同样的问题,这就是我最终要做的事情:

// First set the back button text to an empty string
viewController.navigationItem.backBarButtonItem = 
     [[UIBarButtonItem alloc] initWithTitle:@"" 
          style:UIBarButtonItemStylePlain 
          target:nil 
          action:nil];

// Set the back-indicator image to the custom image (scaled to 20x20)
UIImage *img = [MyUtils imageWithImage:[UIImage imageNamed:@"back"] scaledToSize:CGSizeMake(20, 20)];
[[UINavigationBar appearance] setBackIndicatorImage:img];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:img];

// Set the tint color to WHITE
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

这是图像缩放方法:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

上述缩放方法的信用在此问题上转到Paul Lynch :( The simplest way to resize an UIImage?

答案 7 :(得分:0)

Swift 4(如果你只想隐藏它)

let mask = UIImage()
navigationController?.navigationBar.backIndicatorImage = mask
navigationController?.navigationBar.backIndicatorTransitionMaskImage = mask