iOS调整导航条BarButtonItem与titleView 的间距

简介: iOS调整导航条BarButtonItem与titleView 的间距

前言

例子:调整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 导航条的左边按钮的位置

image.png

- (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];
        }
    }
}

IV、see also


目录
相关文章
|
iOS开发
iOS MFMessageComposeViewController不显示取消按钮,导航条上白色,无取消按钮,无法返回应用...
iOS MFMessageComposeViewController不显示取消按钮,导航条上白色,无取消按钮,无法返回应用...
63 0
|
6月前
|
iOS开发
SwiftUI适配iOS16导航控制器引起的闪退
SwiftUI适配iOS16导航控制器引起的闪退
72 0
|
iOS开发
iOS 15后设置导航控制器的导航条背景色无效的问题处理
iOS 15后设置导航控制器的导航条背景色无效的问题处理
475 0
|
iOS开发
IOS15上纯代码布局之导航控制器的导航条为透明的问题
IOS15上纯代码布局之导航控制器的导航条为透明的问题
223 0
|
iOS开发
iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)
iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)
136 0
iOS开发 - 渐变导航条升级版(判断滚动的方向和改变方向时的位置)
|
安全 iOS开发 开发者
ios应用列表调整后排名规则
ios应用列表调整后排名规则
|
iOS开发
iOS开发-调整文字之间间距
iOS开发-调整文字之间间距
291 0
|
iOS开发
IOS调用导航
IOS调用导航
67 0
|
算法 iOS开发
iOS视图滚动的时候控制导航条标题及公告视图的alpha(显示与隐藏)
iOS视图滚动的时候控制导航条标题及公告视图的alpha(显示与隐藏)
241 0
iOS视图滚动的时候控制导航条标题及公告视图的alpha(显示与隐藏)