如何设置自定义视图后退按钮

时间:2014-05-27 12:43:06

标签: ios objective-c ios7

在View Controller A中,在viewDid Load中我有:

UIImage * backButtonImage = [UIImage imageNamed: @"bar-arrow"];
UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = b;

仍然在下一个视图控制器的导航中。吧我明白了:

enter image description here

换句话说 - IOS7的原始后退箭头仍然显示。无法摆脱它!!

2 个答案:

答案 0 :(得分:1)

通常隐藏NativationBar的默认后退按钮:

[self.navigationItem setHidesBackButton:YES animated:YES];   
[self.navigationItem setHidesBackButton:YES];

并执行以下代码:

    UIButton *BackButton =  [UIButton buttonWithType:UIButtonTypeCustom];
    [BackButton setImage:[UIImage imageNamed:@"bar-arrow"] forState:UIControlStateNormal];
    [BackButton addTarget:self action:@selector(prevButtonAction)forControlEvents:UIControlEventTouchUpInside];
    [BackButton setFrame:CGRectMake(0, 0, 12, 20)];// set as par your need


    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:BackButton];
    self.navigationItem.leftBarButtonItem = barButton;


-(void)prevButtonAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

答案 1 :(得分:0)

如果您想保留后退按钮功能,只需更改V形图像(如果您只定位iOS 7.0及更高版本),则可以更改图像:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationController.navigationBar.backIndicatorImage = [UIImage imageNamed:@"bar-arrow"];
    self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"bar-arrow-transition-mask"];
}

或者如果您要将UINavigationController子类化,我建议:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationBar.backIndicatorImage = [UIImage imageNamed:@"bar-arrow"];
    self.navigationBar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"bar-arrow-transition-mask"];
}

...其中“条形箭头过渡掩模”图像是表示另一图像的可见性的图像,“条形箭头”。在此蒙版图像中,alpha为零的像素将在过渡期间完全隐藏移动的标题,而alpha为1的像素将完全显示移动的标题。

虽然这需要您创建一个新图像,但它应该非常快速和容易制作,并且此解决方案保留所有导航栏功能,而不会弄乱leftBarButtonItemleftBarButtonItems并添加手动拨打popViewControllerAnimated: