如何设置自定义导航控制器全局返回按钮
1个回答
展开全部
我们可以在push的时候来设置这个自定义按钮,push有一个方法判袭
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{}
在这个方法中viewController是push的对象
但是要判断这个viewController是不是根控制器
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.childViewControllers.count > 0) { // 非根控制器
// 非根控制器才需要设置返回按旦冲橘钮
// 设置返回按钮
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTitle:@"返回" forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:@"navigationButtonReturn"]forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:@"navigationButtonReturnClick"]forState:UIControlStateHighlighted];
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[backButton sizeToFit];
// 注意:一定要在按钮内容有尺寸的时候,设置才有效果
backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
// 设置返回按钮
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
}
// 这个方法才是真正模团执行跳转
[super pushViewController:viewController animated:animated];
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{}
在这个方法中viewController是push的对象
但是要判断这个viewController是不是根控制器
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.childViewControllers.count > 0) { // 非根控制器
// 非根控制器才需要设置返回按旦冲橘钮
// 设置返回按钮
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTitle:@"返回" forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:@"navigationButtonReturn"]forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:@"navigationButtonReturnClick"]forState:UIControlStateHighlighted];
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[backButton sizeToFit];
// 注意:一定要在按钮内容有尺寸的时候,设置才有效果
backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
// 设置返回按钮
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
}
// 这个方法才是真正模团执行跳转
[super pushViewController:viewController animated:animated];
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询