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 ...
随机推荐
- yii2源码学习笔记(五)
Event是所有事件类的基类.它封装了与事件相关的参数. yii2\base\Event.php <?php /** * @link http://www.yiiframework.com/ * ...
- Count The Carries
hdu:http://acm.hdu.edu.cn/showproblem.php?pid=4588 题意:给你 a,b两个数,然后让a到b之间的数做2进制的加法,问你与多少次进位.例如:1,3,1+ ...
- DB2 SQL RR/RS/CS/UR四个级别《转载》
1.RR隔离级别:在此隔离级别下, DB2会锁住所有相关的纪录.在一个SQL语句执行期间,所有执行此语句扫描过的纪录都会被加上相应的锁.具体的锁的类型还是由操作的类型来决定,如果是读取,则加共享锁:如 ...
- 想做Android Wear开发?你得先搞明白这四件事
手环和手表的腕上穿戴之争,随着Apple Watch发布和Android Wear不断完善而告一段落.尽管续航上略有缺陷,但手表以其类似手机可扩展的生态环境赢得了众多巨头的支持. Google曾透露, ...
- java dump
注意,请不要被我误导,我没有看其他资料,这是我自己分析的,有些可能是不对的 "DestroyJavaVM" prio=6 tid=0x00316800 nid=0x448 wait ...
- -_-#【工具】图片导出PDF
- Qt入门(11)——Qt插件
Qt提供了一个简单地插件接口,可以轻松地生成作为独立组件的定制数据库驱动.图象格式.文本编解码器(text codec).风格(style)和部件.警告:Qt 3.0.5对插件的一些方面做了改变,具体 ...
- ACM生活总结
两年ACM生活总结 转眼已经踏入ACM这条不归路已经两年了, 深深的感觉到ACM的不易 和 艰辛,但同时ACM给我所带来的快乐,让我认为值一切都是值得的. 我刚上大学那会,我们学校的ACM刚刚起步不到 ...
- POJ 2594 Treasure Exploration(带交叉路的最小路径覆盖)
题意: 派机器人去火星寻宝,给出一个无环的有向图,机器人可以降落在任何一个点上,再沿着路去其他点探索,我们的任务是计算至少派多少机器人就可以访问到所有的点.有的点可以重复去. 输入数据: 首先是n和 ...
- 【模拟】Codeforces 691A Fashion in Berland
题目链接: http://codeforces.com/problemset/problem/691/A 题目大意: n个数0或1,要求恰好n-1个1,如果n为1则那个数一定要是1 题目思路: [模拟 ...