标签:
自我感觉系统提供的BackButton还是很简洁,美观的。但如果要自己定制BackButton,就需要注意很多的细节问题,一下介绍两种自定制的方式:
一:设置BackButton的背景图片,这种方法的优点是不会破坏系统的抽屉效果,不需要自己实现返回上个页面的Pop方法,但是它的缺点是如果图片的大小和backButton要求的大小不一致就会将图片拉伸,这个时候就会很难看,具体代码实现过程如下:(注意这是在上级页面中实现的)
1 // 设置返回键的样式 2 UIImage *normalImage = [[UIImage imageNamed:@"btn_backItem"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, normalImage.size.width,0, 0)]; 3 4 [[UIBarButtonItem appearance]setBackButtonBackgroundImage:normalImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 5 6 // 隐藏标题 7 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault]; 8 9 [self.navigationController pushViewController:_controller animated:YES];
二:自定制leftBarButton来实现同样的效果,缺点:程序繁琐,破坏了系统的抽屉效果,优点:可以自己定义返回键的任意形式,实现的代码如下:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; // 给button设置图片 UIImage *iamge = [UIImage imageNamed:@"btn_backItem"]; UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.frame = CGRectMake(0, 0, 30, 30); [backButton setBackgroundImage:iamge forState:UIControlStateNormal]; // 给button添加事件 [backButton addTarget:self action:@selector(skipToViewController) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *batButton = [[UIBarButtonItem alloc]initWithCustomView:backButton]; self.navigationItem.leftBarButtonItem = batButton; // 确保系统的抽屉效果是有用的 self.navigationController.interactivePopGestureRecognizer.enabled = YES; self.navigationController.interactivePopGestureRecognizer.delegate = nil; } - (void)skipToViewController { [self.navigationController popToRootViewControllerAnimated:YES]; }
标签:
原文地址:http://www.cnblogs.com/Mike-Fighting/p/4731584.html