我有一个导航控制器应用程序,我需要为导航后栏按钮设置自定义操作。尝试了一些解决方法,但尚未找到解决方案。
尝试
UIBarButtonItem *backBarItem = self.navigationItem.leftBarButtonItem;
backBarItem.target = self;
backBarItem.action = @selector(popToHomeViewController);
结果:无效。返回按钮在导航堆栈中仅在先前的ViewController中弹出
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:backBarItem.title style:backBarItem.style target:self action:@selector(popViewController)];
self.navigationItem.leftBarButtonItem = customBarItem;
结果:无效。返回按钮在导航堆栈中仅在先前的ViewController中弹出
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:backBarItem.style target:self action:@selector(popViewController)];
self.navigationItem.leftBarButtonItem = customBarItem;
结果:现在,我的选择器被完美地调用并导航到所需的ViewController。这里的问题是,后按钮不像本机回按钮那样。它没有大胆的"<"我没有提到的角色。如果添加<字符需要更改iOS 6兼容性。
有更好的解决方案,以确保iOS 6和iOS 7与自定义选择器兼容导航返回按钮?
尝试此简单示例将帮助您..
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:@"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
}
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
确保您的资源文件夹中的导航栏返回按钮的大小带有名称back.png
如果需要其他帮助,请随时。
快乐的编码!!!!!
尝试此
viewController.navigationItem.hidesBackButton = YES;
//set custom image to button if needed
UIImage *backButtonImage = [UIImage imageNamed:@"back"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:backButtonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[button addTarget:viewController action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height)];
[backButtonView addSubview:button];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
viewController.navigationItem.leftBarButtonItem = customBarItem;
和在back
方法中您可以自定义
- (void)back {
//Your code
}