开发过程中,发现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. ecshop--加载初始化文件

    define('IN_ECS', true);require(dirname(__FILE__) . '/../../includes/init.php'); 在开头要加入这两句文件才可以访问数据库以 ...

  2. 安卓SDK Manager自动管理各种包

    安卓ADT不能自动从google下载sdk等各种工具 修改hosts文件下载成功 210.242.125.89  dl-ssl.google.com 210.242.125.89  dl.google ...

  3. WWDC2014之App Extensions学习笔记

    一.关于App Extensions extension是iOS8新开放的一种对几个固定系统区域的扩展机制,它可以在一定程度上弥补iOS的沙盒机制对应用间通信的限制. extension的出现,为用户 ...

  4. 小A项目为什么加班

    1.负责架构搭建的人搭建完成架构后,没有进行落地性验证:导致真正要用到的时候才发现spring没有配置:需要对架构人员的进度和内容进行跟踪,在跟踪进度的时候需要强调落地性: 2.负责架构搭建的人没有提 ...

  5. 转:LESS CSS 框架简介

    原文来自于:http://www.ibm.com/developerworks/cn/web/1207_zhaoch_lesscss/ 简介 CSS(层叠样式表)是一门历史悠久的标记性语言,同 HTM ...

  6. Java吸收换行符

            今天做题遇到的--         由于读入的字符串可能包含空格,所以采用nextLine. int n = sc.nextInt(); for(int i=0; i<n; i+ ...

  7. The top 100 papers Nature explores the most-cited research of all time.

    The top 100 papers Nature explores the most-cited research of all time. The discovery of high-temper ...

  8. Delphi XE6 for Android 让手机震动(调用Java的函数)

    震动,是调用了 安卓api  JNI 里面的 函数  ,这些都是 调用java的 ,如下面的引用, uses  FMX.Helpers.Android,  Androidapi.JNI.App,  A ...

  9. 用Java实现非阻塞通信

    用ServerSocket和Socket来编写服务器程序和客户程序,是Java网络编程的最基本的方式.这些服务器程序或客户程序在运行过程中常常会阻塞.例如当一个线程执行ServerSocket的acc ...

  10. 【算法Everyday】第三日 KMP算法

    题目 你知道的. 分析 分析不来. 代码 void OutputArray(int* pArr, int iLen) { ; i < iLen; i++) { printf("%d\t ...