在 iOS7 中删除后退箭头
Posted
技术标签:
【中文标题】在 iOS7 中删除后退箭头【英文标题】:Remove back arrow in iOS7 【发布时间】:2013-12-12 04:36:52 【问题描述】:我想加入一个自定义后退按钮 - 我可以使用
获得上述结果self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back-btn"] style:UIBarButtonItemStylePlain target:nil action:nil];
但是如何移除原生的蓝色按钮呢?
【问题讨论】:
【参考方案1】:使用下面的代码隐藏后退箭头:
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]];
【讨论】:
删除所有箭头,但添加“返回”文本。 如果你添加了backbarbuttonitem,那么它不会显示任何“Back”文本【参考方案2】:在ios7+中你可以使用navigationBar的backIndicatorImage
和backIndicatorTransitionMaskImage
来实现你想要的自定义箭头。 Swift 代码如下:
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named:"button-backArrow18x15")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named:"button-backArrowMask18x15")
如果要隐藏“后退”文本,可以将前一个视图的title
设置为单个空格“”或使用没有标题的自定义UIBarButtonItem
。
【讨论】:
你可能需要使用UIImage(named: "back-arrow")?.withRenderingMode(.alwaysOriginal)
来渲染原图。【参考方案3】:
你可以隐藏后退按钮
self.navigationItem.hidesBackButton=YES;
要创建自定义左栏按钮,您可以使用:
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];
【讨论】:
完全隐藏,我想显示自定义的白色箭头。 为此,您必须创建 Custon LeftBarButton 项目。并设置为左栏按钮项。 我还添加了代码以创建自定义 leftebarbuttonitem。【参考方案4】:检查这个:
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];
【讨论】:
其实比我发现的要简单,看我的回答 @daidai 我将其作为自定义视图,因此您也可以将其设置为框架或绑定,但在您的代码中我们没有设置它。【参考方案5】:感谢所有回答者 - 找到了一种更简单的方法......
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];
【讨论】:
值得注意的是,使用此方法您将失去弹出滑动手势 同样值得注意的是,这是在做一些完全不同的事情。您正在从顶部控制器设置后退按钮,而不是实际返回的控制器。您必须对位于顶部的任何控制器执行此操作...【参考方案6】:您为什么不能尝试使用自定义 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;
【讨论】:
【参考方案7】:我刚刚遇到了完全相同的问题,这就是我最终要做的:
// 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 关于这个 SO 问题:(The simplest way to resize an UIImage?)
【讨论】:
【参考方案8】:Swift 4(如果你只想隐藏它)
let mask = UIImage()
navigationController?.navigationBar.backIndicatorImage = mask
navigationController?.navigationBar.backIndicatorTransitionMaskImage = mask
【讨论】:
以上是关于在 iOS7 中删除后退箭头的主要内容,如果未能解决你的问题,请参考以下文章