zoukankan      html  css  js  c++  java
  • ios中navigationItem的titleView如何居中

    开发过程中,发现titleview很难居中,通过各种尝试终于找到了解决方法。

    首先清楚你个概念:

    1.leftBarButtonItem,导航条中左侧button。

    2.rightBarButtonItem,导航条中右侧button。

    3.titleview,不用介绍了吧,就是标题。

    问题原因:

    经过尝试,发现titleview的起点位置和尺寸依赖于leftBarButtonItem和rightBarButtonItem的位置。

    解决方案:

    设置titleview之前,先初始化leftBarButtonItem和rightBarButtonItem的位置,然后根据leftBarButtonItem和rightBarButtonItem的位置来使titleview居中。

    //以下使参考代码。

    //必须放在 leftBarButtonItemrightBarButtonItem初始化之后调用

    - (void)setDisplayCustomTitleText:(NSString*)text

    {

        // Init views with rects with height and y pos

        UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

        // Use autoresizing to restrict the bounds to the area that the titleview allows

        titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

        titleView.autoresizesSubviews = YES;

        titleView.backgroundColor = [UIColorclearColor];

        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

        titleLabel.tag = kUIVIEWCONTROLLER_LABEL_TAG;

        titleLabel.backgroundColor = [UIColor clearColor];

        titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];

        titleLabel.textAlignment = UITextAlignmentCenter;

        titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        titleLabel.textColor = TC_CNavigationTitleColor;

        titleLabel.lineBreakMode = UILineBreakModeClip;

        titleLabel.textAlignment = UITextAlignmentCenter;

        titleLabel.autoresizingMask = titleView.autoresizingMask;

        

        CGRect leftViewbounds = self.navigationItem.leftBarButtonItem.customView.bounds;

        CGRect rightViewbounds = self.navigationItem.rightBarButtonItem.customView.bounds;

        

        CGRect frame;

        CGFloat maxWidth = leftViewbounds.size.width > rightViewbounds.size.width ? leftViewbounds.size.width : rightViewbounds.size.width;

        maxWidth += 15;//leftview 左右都有间隙,左边是5像素,右边是8像素,加2个像素的阀值 5 8 2

        

        frame = titleLabel.frame;

        frame.size.width = 320 - maxWidth * 2;

        titleLabel.frame = frame;

        

        frame = titleView.frame;

        frame.size.width = 320 - maxWidth * 2;

        titleView.frame = frame;

        // Set the text

        titleLabel.text = text;

            // Add as the nav bar's titleview

        [titleView addSubview:titleLabel];

        self.navigationItem.titleView = titleView;

        [titleView release];

        [titleLabel release];

    }

  • 相关阅读:
    promise封装微信小程序的request
    css3实现loading效果--当页面加载过程中显示Loading的进度条,全部加载完成之后进度条消失
    解决H5微信浏览器中audio兼容-- 背景音乐无法自动播放
    h5微信浏览器复制粘贴--ios兼容问题的解决方法(clipboard.js插件)
    解决微信二次分享失败--后面被加上from=singlemessage&isappinstalled=0的解决方案
    h5微信分享
    mpvue微信小程序项目踩坑记录
    水塘抽样算法
    Redis批量删除脚本
    java9 模块化 jigsaw
  • 原文地址:https://www.cnblogs.com/max5945/p/3225587.html
Copyright © 2011-2022 走看看