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

首先清楚你个概念:

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

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

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

问题原因:

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

解决方案:

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

//以下使参考代码。

//必须放在 leftBarButtonItem和rightBarButtonItem初始化之后调用

- (void)setDisplayCustomTitleText:(NSString*)text

{

    // Init views with rects with height and y pos

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

    // 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(, , , )];

    titleLabel.tag = kUIVIEWCONTROLLER_LABEL_TAG;

    titleLabel.backgroundColor = [UIColor clearColor];

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

    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 += ;//leftview 左右都有间隙,左边是5像素,右边是8像素,加2个像素的阀值 5 + 8 + 2

    frame = titleLabel.frame;

    frame.size.width =  - maxWidth * ;

    titleLabel.frame = frame;

    frame = titleView.frame;

    frame.size.width =  - maxWidth * ;

    titleView.frame = frame;

    // Set the text

    titleLabel.text = text;

        // Add as the nav bar's titleview

    [titleView addSubview:titleLabel];

    self.navigationItem.titleView = titleView;

}

iOS中navigationItem的titleView如何居中的更多相关文章

  1. IOS Custom NavigationItem --写titleView

    //先自己写一个titleView UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];//all ...

  2. iOS中navigationItem修改标题的颜色

    UIColor * color = [UIColor redColor];//这里我们设置的是颜色,NSDictionary * dict = [NSDictionary dictionaryWith ...

  3. iOS:iOS中的多控制器管理

    iOS中的控制器有三种创建方式: 1.通过storyboard创建 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@" ...

  4. iOS中多控制器的使用

    通常情况下,一个app由多个控制器组成,当app中有多个控制器的时候,我们就需要对这些控制器进行管理. 在开发过程中,当有多个View时,可以用一个大的view去管理多个小的view,控制器也是如此, ...

  5. ios 中的小技巧 - 总有你想要的 一

    UITableView的Group样式下顶部空白处理 在viewWillAppear里面添加如下代码: //分组列表头部空白处理 CGRect frame = myTableView.tableHea ...

  6. 【转】iOS中设置导航栏标题的字体颜色和大小

    原文网址:http://www.360doc.com/content/15/0417/11/20919452_463847404.shtml iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参 ...

  7. iOS中设置导航栏标题的字体颜色和大小

    iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参考下. 在平时开发项目的时候,难免会遇到修改导航栏字体大小和颜色的需求,一般使用自定义视图的方法,其实还存在一种方法. 方法一:(自定义视图的 ...

  8. ios中的category与extension

    http://blog.csdn.net/haishu_zheng/article/details/12873151   category和extension用来做类扩展的,可以对现有类扩展功能或者修 ...

  9. [BS-25] IOS中手势UIGestureRecognizer概述

    IOS中手势UIGestureRecognizer概述 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touches ...

随机推荐

  1. STM32学习笔记——定时器中断(向原子哥学习)

    定时器中断 STM32 的定时器功能十分强大,有 TIME1 和 TIME8 等高级定时器,也有 TIME2~TIME5 等通用定时器,还有 TIME6 和TIME7 等基本定时器.在本章中,我们将利 ...

  2. CentOS下命令行和桌面模式的切换方法(转载)

    桌面模式和命令行模式的切换方法 用编辑器打开 /etc/inittab 文件(这里用的是vi,你可以选择你喜欢的): #vi /etc/inittab 打开效果图如下: 桌面模式    : 把光标所在 ...

  3. java项目导出jar文件时指定main方法的类

    需要先运行一下main函数,eclipse的Export-->Runnable JAR File ---> 下的Launch configuration下拉列表才会有记录.如果想要删除下拉 ...

  4. Linux查看端口号

    查看端口占用情况的命令:lsof -i 查看某一端口的占用情况: lsof -i:端口号 结束占用端口的进程:killall 进程名 也可使用命令: netstat -apn|grep <端口号 ...

  5. linux学习笔记---一些有趣的命令

    一 在说链接之前我们哈需要说明一个东西,就是inode,一个文件的名字可以有多个,但是inode里的i-number却只有一个,(inode是一个数据结构,里面存放文件的各种属性,属主,属组,权限,大 ...

  6. 【译】UI设计基础(UI Design Basics)--iOS应用解析(iOS App Anatomy)(三)

    2.1  iOS应用解析(iOS App Anatomy) 几乎所有的iOS应用都会用到UIKit框架中的组件.了解这些基础组件的名称,角色,功能可以帮你在应用界面设计时做出更好的决策. UIKit提 ...

  7. fread遇到1A则读取停止,发现是1A是文件截止符

    fread遇到1A则读取停止,发现是1A是文件截止符 按二进制来读来则可以完全读取

  8. W3C vs. WHATWG HTML5 Specs – The Differences Documented

    A few weeks ago, HTML5 became an official W3C Recommendation. I took advantage of this event to disc ...

  9. Ombrophobic Bovines

    poj2391:http://poj.org/problem?id=2391 题意:一个人有n个农场,每个农场都一个避雨的地方,每个农场有一些牛,每个避雨的地方能容纳牛的数量是有限的.农场之间有一些道 ...

  10. POJ-3693-Maximum repetition substring(后缀数组-重复次数最多的连续重复子串)

    题意: 给出一个串,求重复次数最多的连续重复子串 分析: 比较容易理解的部分就是枚举长度为L,然后看长度为L的字符串最多连续出现几次. 既然长度为L的串重复出现,那么str[0],str[l],str ...