今天在做项目的时候遇到一点小问题,后来自己解决了,也顺便思考了一下更深层次的东西,在这里分享一下~~
替换返回按钮的文字
很多app的要求所有的返回按钮的title都是“返回”,如果上一层题目文字太多,下一层的返回按钮文字就会显示不完全,而且这样可以使软件显得整洁。
方法一:
最普通的想法,A界面的navigationItem.backBarButtonItem修改的是B(A的下一级)界面的返回按钮,所以可以写一个BaseViewController,让所有的UIViewController继承它,像这样
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
}
方法二:
思路是:在开启程序时,使用Method Swizzling,将系统自带的backBarButtonItem方法替换成我们自定义的方法。
具体实现就是创建个 UINavigationItem + BackItem 的类别,当系统执行+load方法时,将backBarButtonItem方法和myCustomBackButton方法交换,利用iOS关联方法(objc_getAssociatedObject,objc_setAssociatedObject)在static区创建个backItemButton用来展示。这个方法的好处就是将类别导入到工程里不用任何代码就可以实现,上代码吧:
复制代码
+(void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method originalMethodImp = class_getInstanceMethod(self, @selector(backBarButtonItem));
Method destMethodImp = class_getInstanceMethod(self, @selector(myCustomBackButton));
method_exchangeImplementations(originalMethodImp, destMethodImp);
});
}
static char kCustomBackButtonKey;
-(UIBarButtonItem *)myCustomBackButton{
UIBarButtonItem *item = [self myCustomBackButton];
if (item) {
return item;
}
item = objc_getAssociatedObject(self, &kCustomBackButtonKey);
if (!item) {
item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:NULL];
objc_setAssociatedObject(self, &kCustomBackButtonKey, item, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return item;
}
- (void)dealloc
{
objc_removeAssociatedObjects(self);
}
复制代码
返回按钮只显示自定义图片
方法一:
最开始我的方案是实现UINavigationController的代理判断是否是栈底,如果不是就修改leftBarButtonItem的样式。
复制代码
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
UIViewController *root = navigationController.viewControllers[0];
if (root != viewController) {
UIBarButtonItem *itemleft = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backBtn"] style:UIBarButtonItemStylePlain target:self action:@selector(popAction:)];
viewController.navigationItem.leftBarButtonItem = itemleft;
}
}
- (void)popAction:(UIBarButtonItem *)barButtonItem
{
[self.navigationController popViewControllerAnimated:YES];
}
复制代码
但iOS7.0出了之后,这并不是一个好的解决方案。因为iOS7.0之后如果把backItemButton替换成leftItemButon,interactivePopGestureRecognizer手势就没有了。虽然可以使用
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
方法二:
这个方法和上边‘替换返回按钮的文字’方法二大致一样,这句话要替换:
item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:nil action:nil];
这句话要加上,用来去除系统自带的返回按钮上的尖脚号。
[[UINavigationBar appearance] setBackIndicatorImage:[[UIImage alloc]init]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[[UIImage alloc] init]];
技术上的问题其实有好多方法可以去解决,而且在效率相差不大的情况下,关键是我们的使用习惯,所以整理一下现在的思路,方便以后使用。
http://www.zaojuzi.com/paibiju/1223.html http://www.zaojuzi.com/paibiju/1161.html
http://www.zaojuzi.com/biyuju/2671.html
http://www.zaojuzi.com/biyuju/2458.html
http://www.zaojuzi.com/biyuju/2457.html
http://www.zaojuzi.com/nirenju/2175.html
http://www.zaojuzi.com/nirenju/2153.html
http://www.zaojuzi.com/nirenju/2149.html
http://www.zaojuzi.com/content/1198.html
http://www.zaojuzi.com/content/1187.html
http://www.zaojuzi.com/content/1188.html
http://www.zaojuzi.com/biyuju/2453.html
http://www.zaojuzi.com/ciyuzaoju/8983.html
http://www.zaojuzi.com/ciyuzaoju/8981.html
http://www.zaojuzi.com/ciyuzaoju/8972.html
http://www.zaojuzi.com/ciyuzaoju/8970.html
http://www.zaojuzi.com/zuci/13693.html
http://www.zaojuzi.com/zuci/13692.html
http://www.zaojuzi.com/zuci/13691.html
http://www.zaojuzi.com/zuci/13690.html
http://www.zaojuzi.com/zuci/13689.html
http://www.zaojuzi.com/zuci/13688.html
http://www.zaojuzi.com/zuci/13687.html
http://www.zaojuzi.com/zuci/13686.html
http://www.zaojuzi.com/zuci/13685.html
http://www.zaojuzi.com/zuci/13684.html
http://www.zaojuzi.com/zuci/13683.html
http://www.zaojuzi.com/mingyanmingju/4422.html
http://www.zaojuzi.com/mingyanmingju/4420.html
http://www.zaojuzi.com/mingyanmingju/4590.html
http://www.zaojuzi.com/mingyanmingju/4433.html
http://www.zaojuzi.com/chengyuzaoju/12202.html
http://www.zaojuzi.com/chengyuzaoju/12042.html
http://www.zaojuzi.com/chengyuzaoju/12041.html
http://www.zaojuzi.com/guanliancizaoju/15690.html
http://www.zaojuzi.com/guanliancizaoju/13545.html
http://www.zaojuzi.com/guanliancizaoju/13541.html
http://www.zaojuzi.com/guanliancizaoju/13537.html
http://www.zaojuzi.com/content/1171.html
http://www.zaojuzi.com/content/1172.html
http://www.zaojuzi.com/content/1177.html
http://www.zaojuzi.com/content/1155.html
http://www.zaojuzi.com/content/1169.html
http://www.zaojuzi.com/content/1146.html
http://www.zaojuzi.com/content/1119.html
http://www.zaojuzi.com/content/1103.html
http://www.zaojuzi.com/content/1081.html
http://www.zaojuzi.com/content/1073.html
http://www.zaojuzi.com/content/1074.html
http://www.zaojuzi.com/content/1028.html
http://www.zaojuzi.com/content/page_3.html
http://www.zaojuzi.com/content/page_4.html
http://www.zaojuzi.com/content/page_5.html
http://www.zaojuzi.com/content/page_6.html
http://www.zaojuzi.com/content/page_7.html
http://www.zaojuzi.com/content/page_67.html
http://www.zaojuzi.com/nirenju/1185.html
http://www.zaojuzi.com/nirenju/2246.html
http://www.zaojuzi.com/nirenju/2241.html
http://www.zaojuzi.com/nirenju/2251.html
http://www.zaojuzi.com/nirenju/page_2.html
http://www.zaojuzi.com/nirenju/page_3.html
http://www.zaojuzi.com/nirenju/page_4.html
http://www.zaojuzi.com/biyuju/2452.html
http://www.zaojuzi.com/biyuju/3751.html
http://www.zaojuzi.com/biyuju/3298.html
http://www.zaojuzi.com/biyuju/3284.html
http://www.zaojuzi.com/biyuju/3406.html
http://www.zaojuzi.com/biyuju/3405.html
http://www.zaojuzi.com/biyuju/3404.html
http://www.zaojuzi.com/biyuju/8936.html
http://www.zaojuzi.com/biyuju/5505.html
http://www.zaojuzi.com/biyuju/8932.html
http://www.zaojuzi.com/biyuju/659.html
http://www.zaojuzi.com/biyuju/page_2.html
http://www.zaojuzi.com/biyuju/page_3.html
http://www.zaojuzi.com/biyuju/page_4.html
http://www.zaojuzi.com/kuazhangju/7357.html
http://www.zaojuzi.com/kuazhangju/1658.html
http://www.zaojuzi.com/fanwenju/7084.html