iOS navigationBar自定义返回按钮
先看代码
UIImage *img = [[UIImage imageNamed:@"nav_back_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleDone target:self action:@selector(leftBarBtnClicked)];
self.navigationItem.leftBarButtonItem = leftItem;
再看代码
UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
leftBtn.frame = CGRectMake(0, 0, 25,25);
UIImage *img = [[UIImage imageNamed:@"nav_back_black"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[leftBtn setImage:img forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(leftBarBtnClicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
上面这两种方式其实是差不多的,只是不同的方式初始化UIBarButtonItem而已。
代码放上去之后发现,展示效果很好。第一种方式看起来大概是正常的,除非遇到奇葩的设计师非要要求箭头距左边间距;第二种方式可能返回箭头位置不是你想要的,自己调调frame就好。反正位置调不好的再自行百度吧,教程很多。
自定义返回按钮后,右滑返回失效。解决方案:
方案1:
//重新设置手势的delegate
self.navigationController.interactivePopGestureRecognizer.delegate= (id)self;
自己响应这个手势的事件
[self.navigationController.interactivePopGestureRecognizeraddTarget:self action:@selector(handleGesture:)];
方案2:
self.interactivePopGestureRecognizer.enabled = YES;
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
self.interactivePopGestureRecognizer.delegate = nil;
}
网友评论