开发过程中,发现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. js 去除字符串开头或者前几个字符。slice 也可以用于截取某一部分

    摘草自w3 slice() 方法可从已有的数组中返回选定的元素. 语法 arrayObject.slice(start,end) 参数 描述 start 必需.规定从何处开始选取.如果是负数,那么它规 ...

  2. 谨慎使用php的strtotime()函数

    我们在日常业务中,针对业务量,经常会采用对数据库按时间做横向分表,分表后的查询往往会涉及到时间问题.例如,我们想查询某个用户距离当前时间1个月的订单情况,在这个时候,我们有些会用到strtotime( ...

  3. 使用Raphael 画图(三) 事件 (javascript)

    这章展示事件例子. 下图是官方API的事件: 例子: var butt1 = paper.set(); var a1 = paper.circle(24.833, 26.917, 26.667).at ...

  4. PHPCMS收集标签使用

    调用子栏目(在栏目首页模板需要用到) {pc:content action="category" catid="$catid" num="25&quo ...

  5. KVO初探

    一,概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应 ...

  6. bzoj1563: [NOI2009]诗人小G

    Description Input Output 对于每组数据,若最小的不协调度不超过1018,则第一行一个数表示不协调度若最小的不协调度超过1018,则输出"Too hard to arr ...

  7. C++标准库的数值极限numeric_limits

    包含头文件:#include<limits> 它是一个模板类,它主要是把C++当中的一些内建型别进行了封装,比如说numeric_limits<int>是一个特化后的类,从这个 ...

  8. Unity3D批处理脚本

    Unity3D批处理脚本 本文属于转载,如有侵权,请留言,我会及时删除! Max09在模型到处的模型和U3D场景的尺寸不一致,Max09中的1m导到U3D中,只有0.01m,这时可以在U3D中将模型的 ...

  9. Git初体验

    http://www.cnblogs.com/yejiurui/archive/2013/07/29/3223153.html

  10. QVariant(相当于是Java里面的Object,起到一个数据类型“擦除”的作用,可以使用Q_DECLARE_METATYPE进行注册)

    =QVariant= [%这个类型相当于是Java里面的Object,它把绝大多数Qt提供的数据类型都封装起来,起到一个数据类型“擦除”的作用.比如我们的 table单元格可以是string,也可以是 ...