前言
例子:调整BarButtonItem按钮和titleView的间距
1、原理:
titleview的起点位置和尺寸依赖于leftBarButtonItem和rightBarButtonItem的位置
。2、设置titleview之前,先初始化leftBarButtonItem和rightBarButtonItem的位置,然后根据leftBarButtonItem和rightBarButtonItem的位置来使titleview居中。
常见问题
1、 BarButtonItem 隐藏失效的解决方案:
使用initWithCustomView进行实例化BarButtonItem
2、 iOS13.5.1 版本无法点击导航条右侧按钮:CustomView 不能直接是UIButton, 因此解决方案只要
对UIButton进行包装一层之后再作为CustomView
I、调整BarButtonItem按钮和titleView的间距
与屏幕边界 或者与titleView 的间距 只要分别调整rightBarButtonItems 数组元素的顺序。
主要利用UIBarButtonItem 的UIBarButtonSystemItemFixedSpace 系统控件
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; /** * width为负数时,相当于btn向右移动width数值个像素,由于按钮本身和边界间距为5pix,所以width设为-5时,间距正好调整 * 为0;width为正数时,正好相反,相当于往左移动width数值个像素 */ negativeSpacer.width = 10;
1.1 调整右边按钮和titleView的间距
//设置右边按钮 UIBarButtonItem *btn_right = [UIBarButtonItem barButtonItemWithTarget:self Image:@"in" highlightedImage:@"in" actionMethod:@selector(in)]; UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; /** * width为负数时,相当于btn向右移动width数值个像素,由于按钮本身和边界间距为5pix,所以width设为-5时,间距正好调整 * 为0;width为正数时,正好相反,相当于往左移动width数值个像素 */ negativeSpacer.width = 10; self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: btn_right, negativeSpacer,nil];
1.2 调整左边按钮和titleView的间距
// 经过尝试,发现titleview的起点位置和尺寸依赖于leftBarButtonItem和rightBarButtonItem的位置。 UILabel * centerView = [[UILabel alloc] initWithFrame:CGRectMake(kAdjustRatio(20),0 , self.view.frame.size.width- kAdjustRatio(20), self.navigationController.navigationBar.frame.size.height)]; self.navigationItem.titleView = centerView; // 设置titleview之前,先初始化leftBarButtonItem和rightBarButtonItem的位置,然后根据leftBarButtonItem和rightBarButtonItem的位置来使titleview居中。 // self.navigationItem.titleView. UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; /** * width为负数时,相当于btn向右移动width数值个像素,由于按钮本身和边界间距为5pix,所以width设为-5时,间距正好调整 * 为0;width为正数时,正好相反,相当于往左移动width数值个像素 */ negativeSpacer.width = 10; // self.navigationItem.leftBarButtonItem =negativeSpacer; self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects: negativeSpacer,nil];
1.3 设置webViewVC 导航条的左边按钮的位置
- (void)updateNavigationItems { [self.navigationItem setLeftBarButtonItems:nil animated:NO]; if (self.webView.canGoBack/* || self.webView.backForwardList.backItem*/) {// Web view can go back means a lot requests exist. UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; spaceButtonItem.width = -6.5; self.navigationController.interactivePopGestureRecognizer.enabled = NO; if (self.navigationController.viewControllers.count == 1) { NSMutableArray *leftBarButtonItems = [NSMutableArray arrayWithArray:@[spaceButtonItem,self.navigationBackBarButtonItem]]; // If the top view controller of the navigation controller is current vc, the close item is ignored. if (self.showsNavigationCloseBarButtonItem && self.navigationController.topViewController != self){ [leftBarButtonItems addObject:self.navigationCloseBarButtonItem]; } [self.navigationItem setLeftBarButtonItems:leftBarButtonItems animated:NO]; } else { if (self.showsNavigationCloseBarButtonItem){ [self.navigationItem setLeftBarButtonItems:@[self.navigationCloseBarButtonItem] animated:NO]; }else{ [self.navigationItem setLeftBarButtonItems:@[] animated:NO]; } } } else { self.navigationController.interactivePopGestureRecognizer.enabled = YES; [self.navigationItem setLeftBarButtonItems:nil animated:NO]; } }
II、相关的常见问题
2.1 self.navigationItem.leftBarButtonItem.customView.hidden=YES
失效
- UIBarButtonItem 使用
initWithCustomView
进行实例化的时,这个方法才生效
self.navigationItem.leftBarButtonItem.customView.hidden=YES
- 解决方法:使用
initWithCustomView
进行实例化
UIBarButtonItem *lefttItem = [[UIBarButtonItem alloc]initWithCustomView:btn]; self.navigationItem.leftBarButtonItem = lefttItem;
2.2 iOS13.5.1 版本无法点击导航条右侧按钮
- 无法点击代码
// UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; // [btn setTitle:QCTLocal(@"print") forState:UIControlStateNormal]; // [btn setTitleColor:HWColor(51, 51, 51) forState:UIControlStateNormal]; // btn.titleLabel.font = [UIFont systemFontOfSize:14.0f]; // [btn addTarget:self action:@selector(printBtn:) forControlEvents:UIControlEventTouchUpInside]; // btn.tag = 0; // btn.tintColor = [UIColor blackColor]; // UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:btn]; // rightBtn.tintColor = [UIColor blackColor]; // self.navigationItem.rightBarButtonItem = rightBtn;
解决方案
CustomView 不能直接是UIButton, 因此解决方案只要对UIButton进行包装一层即可
- (void)setupNavigationBar { // 设置导航条右侧的按钮 UIButton *btn = [[UIButton alloc] init]; // [btn setImage:[UIImage imageNamed:@"icon_kaidan_huiyuan"] forState:UIControlStateNormal]; [btn setTitle:QCTLocal(@"print") forState:UIControlStateNormal]; [btn setTitleColor:rgb(51,51,51) forState:UIControlStateNormal]; [btn addTarget:self action:@selector(printBtn:) forControlEvents:UIControlEventTouchDown]; btn.titleLabel.font = kTextFont(14); [btn sizeToFit]; UIView *btnView = [[UIView alloc] init]; btnView.frame = btn.bounds; [btnView addSubview:btn]; UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btnView]; item.tintColor = [UIColor whiteColor]; self.navigationItem.rightBarButtonItem = item; }
2.3 使用 [UIBarButtonItem alloc] initWithImage: 方法,导致图片颜色被冲的解决方案
从其他VC回到当前控制器的时候,发现右边的self.navigationItem.rightBarButtonItem的背景颜色被冲淡了
- 解决方法:UIImageRenderingModeAlwaysOriginal
UIImage *aimage = [UIImage imageNamed:@"icon_right_menu"]; UIImage *image = [aimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(moreAction)]; [self.navigationItem setRightBarButtonItem:rightItem animated:YES];
III、 控制器的跳转和回退细节处理
3.1 UIActionSheet打开相册的处理:通常是等didDismissWithButtonIndex 之后才进行控制器跳转
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 8_3) __TVOS_PROHIBITED{ if (buttonIndex == 0) { [self createCamera]; } }
3.2 pop回栈中指定的VC
- (void)pop { for (UIViewController *temp in self.navigationController.viewControllers) { if ([temp isKindOfClass:[QCTMyFinancialViewController class]]) { [self.navigationController popToViewController:temp animated:YES]; } } }