iOS中navigationItem的titleView如何居中
开发过程中,发现titleview很难居中,通过各种尝试终于找到了解决方法。
首先清楚你个概念:
leftBarButtonItem,导航条中左侧button。
rightBarButtonItem,导航条中右侧button。
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如何居中的更多相关文章
- IOS Custom NavigationItem --写titleView
//先自己写一个titleView UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];//all ...
- iOS中navigationItem修改标题的颜色
UIColor * color = [UIColor redColor];//这里我们设置的是颜色,NSDictionary * dict = [NSDictionary dictionaryWith ...
- iOS:iOS中的多控制器管理
iOS中的控制器有三种创建方式: 1.通过storyboard创建 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@" ...
- iOS中多控制器的使用
通常情况下,一个app由多个控制器组成,当app中有多个控制器的时候,我们就需要对这些控制器进行管理. 在开发过程中,当有多个View时,可以用一个大的view去管理多个小的view,控制器也是如此, ...
- ios 中的小技巧 - 总有你想要的 一
UITableView的Group样式下顶部空白处理 在viewWillAppear里面添加如下代码: //分组列表头部空白处理 CGRect frame = myTableView.tableHea ...
- 【转】iOS中设置导航栏标题的字体颜色和大小
原文网址:http://www.360doc.com/content/15/0417/11/20919452_463847404.shtml iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参 ...
- iOS中设置导航栏标题的字体颜色和大小
iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参考下. 在平时开发项目的时候,难免会遇到修改导航栏字体大小和颜色的需求,一般使用自定义视图的方法,其实还存在一种方法. 方法一:(自定义视图的 ...
- ios中的category与extension
http://blog.csdn.net/haishu_zheng/article/details/12873151 category和extension用来做类扩展的,可以对现有类扩展功能或者修 ...
- [BS-25] IOS中手势UIGestureRecognizer概述
IOS中手势UIGestureRecognizer概述 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touches ...
随机推荐
- ecshop标签大全
页面关键字:{$keywords} 页面标题:{$page_title} 产品分类:父分类列表 {foreach from=$categories item=cat } 父分类超链接 [u ...
- glide简介
golang包管理工具glide简介 golang包管理工具glide简介 前言 golang是一个十分有趣,简洁而有力的开发语言,用来开发并发/并行程序是一件很愉快的事情.在这里我感受到了其中一 ...
- awk里的各种坑
今天又遇到一个,一旦需要定义一个局部数组(awk通过把局部变量定义在函数参数列表来实现局部这一特征)那么这个数组可以正常的操作,但是无法对他取长度,一旦使用length(tempArr)会得到这么一个 ...
- 转:PHP开发框架流行度排名:Laravel居首
原文来自于:http://www.sitepoint.com/best-php-frameworks-2014/ Update: If you’d like to take part in the n ...
- MFC任务管理器task manager----进程的挂起与恢复--NtSuspendProcess&&NtResumeProcess
http://hi.baidu.com/xbbsh/blog/item/b73d3125462201084c088db1.html ---------------------------------- ...
- 【VS调试】C#读写Windows 7注册表时抛出“不允许所请求的注册表访问权”的解决办法
原文:[VS调试]C#读写Windows 7注册表时抛出"不允许所请求的注册表访问权"的解决办法 项目 - 属性 - 安全性,"使用ClickOnce",修改a ...
- 【转】深入了解android平台的jni---注册native函数
原文网址:http://my.oschina.net/u/157503/blog/169041 注册native函数有两种方法:静态注册和动态注册. 1.静态注册方法 根据函数名找到对应的JNI函数: ...
- 组合数学(Pólya计数原理):UvaOJ 10601 Cubes
Cubes You are given 12 rods of equal length. Each of them is colored in certain color. Your task is ...
- Sort List ——LeetCode
Sort a linked list in O(n log n) time using constant space complexity. 链表排序,要求时间复杂度O(nlgn),我写的归并排序. ...
- wchar_t与char、wstring与string的相互转换
个人倾向于使用优秀的开源库做这个. 最近使用boost进行转换,代码极其简单: boost::filesystem::path src(wchar_t); char = src.string().c_ ...