向leftBarButtonItem添加后退箭头?

时间:2014-01-07 12:01:58

标签: ios objective-c ipad uinavigationbar uibarbuttonitem

我想向leftBarButtonItem添加一个后箭头,使其看起来像是一个常规的后退按钮,虽然它的功能略有不同。

有办法做到这一点吗?

2 个答案:

答案 0 :(得分:12)

如果您使用导航控制器,则可以使用自定义按钮并隐藏后退按钮。

要隐藏后退按钮,请使用此选项:

self.navigationItem.hidesBackButton = YES;

对于自定义按钮:

UIButton * customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setBackgroundColor:[UIColor colorWithRed:197.0/255.0 green:190.0/255.0 blue:157.0/255.0 alpha:1.0]];
[customButton setTitle:@"Do Something" forState:UIControlStateNormal];
customButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:11.0f];
[customButton.layer setCornerRadius:3.0f];
[customButton.layer setMasksToBounds:YES];
[customButton.layer setBorderWidth:1.0f];
[customButton.layer setBorderColor: [[UIColor grayColor] CGColor]];
customButton.frame=CGRectMake(0.0, 100.0, 50.0, 25.0);
[customButton addTarget:self action:@selector(customMethod:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * customItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
customItem.tintColor=[UIColor blackColor];
self.navigationItem.leftBarButtonItem = customItem;

这对你有用。 快乐编码

答案 1 :(得分:0)

如果您正在使用UINavigationController,然后按下新的viewcontroller,它将自动显示后退按钮。 如果您不使用 UINavigationController 而不是

使用自定义UIButton进行背面图像

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 48, 37);
[backButton addTarget:self action:@selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
backButton.showsTouchWhenHighlighted = YES;

UIImage *backButtonImage = [UIImage imageNamed:@"back-button.png"];
[backButton setImage:backButtonImage forState:UIControlStateNormal];

backButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;

您可以根据自己的要求更改框架和imageEdgeInsets的值。

参考this SO答案。