自定义导航栏上的返回按钮

mac2022-06-30  66

 

导航栏的按钮,右边的按钮是可以自己随意添加的。但左边的返回按钮怎么定制?

正确的答案是重载UINavigationController类的pushViewController:animated方法。

1 #import @interface MyNavigationController: UINavigationController 2 3 { 4 5 } 6 7 @end 8 9 #import "MyNavigationController.h" 10 11 @implementation MyNavigationController 12 13 -(void)popself { 14 15 [self popViewControllerAnimated:YES]; 16 17 } 18 19 -(UIBarButtonItem*) createBackButton 20 21 { 22 23 return [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(popself)]; 24 25 } 26 27 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { 28 29 [super pushViewController:viewControlleranimated:animated]; 30 31 if (viewController.navigationItem.leftBarButtonItem== nil && [self.viewControllers count] > 1) { 32 33 viewController.navigationItem.leftBarButtonItem =[self createBackButton]; 34 35 } 36 37 } 38 39 @end 使用MyNavigationController替换UINavigationController。

或者直接创建一个UINavigationController的新类别

转载于:https://www.cnblogs.com/w-zhijun/archive/2012/05/14/2499133.html

相关资源:详解微信小程序胶囊按钮返回|首页自定义导航栏功能
最新回复(0)