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 ...
随机推荐
- Atom package安装失败的解决方案
cd ~/.atom/package git clone [package url] cd [package name] apm install [package name] if lack some ...
- opencv之图像膨胀
#include <cv.h> #include <highgui.h> void main() { IplImage* src; IplImage*dst; src=cvLo ...
- 解决maven仓库有jar包但是maven程序无法下载仓库jar包
话说,这个问题困扰了我两个多月了已经~~~ 后来发现不知道被谁动了,把我的仓库没有放到仓库组里面~~~ 用admin登录进去,默认密码是admin123,然后看截图操作吧. (记得删除你本地报错说** ...
- 关于 Boolean 的转换
前端经常喜欢这样写 if else if(value) { //do something } javascript 能智能的把任何类型的 value 转换成 boolean 来进行 if 判断 转换是 ...
- struts2 Convention插件零配置,使用注解开发
从struts21开始,struts2不再推荐使用codebehind作为零配置插件,而是改用Convention插件来支持零配置.与以前相比较,Convention插件更彻底. 使用Conventi ...
- C#里4个访问权限修饰符
C#里类及类成员的修饰符有以下五个如下:public 公开 类及类成员的修饰符 对访问成员没有级别限制private 私有 类成员的修饰符 只能在类的内部访问protected 受保护的 类成员的修饰 ...
- OSGI在Eclipse中执行-console出错的问题
在Eclipse中安装osgi插件后,执行出现异常:
- NOI题库1799 最短前缀
1799:最短前缀 总时间限制: 1000ms 内存限制: 65536kB 描述 一个字符串的前缀是从该字符串的第一个字符起始的一个子串.例如 "carbon"的字串是: &quo ...
- Mac下Intellij IDea发布Java Web项目详解三 新建Module
准备工作1:新建第一个JavaWeb项目 Step4 添加两个module 4.1 右键[WebWorkSpace]-[New]-[Module] 4.2 重复 准备工作1:新建第一个JavaWeb项 ...
- [转载]软件测试之Web测试经典总结
转载自:软件测试之Web测试经典总结 基于Web的系统测试在基于Web的系统开发中,如果缺乏严格的过程,我们在开发.发布.实施和维护Web的过程中,可能就会碰到一些严重的问题,失败的可能性很大.而且, ...