iOS 设置titleview的宽度为屏幕宽
2015-10-24 17:00
330 查看
项目中,需要使用self.navigationItem.titleView来设置titleview,并且要求达到和屏幕一样宽。
单纯的设置frame,没有达到预想的效果,效果图如下:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/04/ca3fd50522f267e545ca52aafef58c48.png)
两边总是留出部分“空隙”。查找相关资料,达到的解决方案如下:
继承UIView, 重写其中的setFrame方法。
使用代码:
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; label.text = @"月未央"; label.textAlignment = NSTextAlignmentCenter; label.backgroundColor = [UIColor redColor]; self.navigationItem.titleView = label;
单纯的设置frame,没有达到预想的效果,效果图如下:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202011/04/ca3fd50522f267e545ca52aafef58c48.png)
两边总是留出部分“空隙”。查找相关资料,达到的解决方案如下:
继承UIView, 重写其中的setFrame方法。
@implementation GFTitleView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { } return self; } - (void)setFrame:(CGRect)frame { [super setFrame:CGRectMake(0, 0, self.superview.bounds.size.width, self.superview.bounds.size.height)]; }
使用代码:
titleView = [[GFTitleView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; titleView.backgroundColor = [UIColor redColor]; self.navigationItem.titleView = titleView;
相关文章推荐
- 【iOS】The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods install
- iOS Xcode工程目录的 folder 和 group的区别(蓝色和黄色文件夹的区别)
- iOS可变参数实现及原理剖析
- iOS应用生命周期详解
- iOS多线程2 Opertaion补充
- iOS经常使用快捷键
- iOS Good Practices
- IOS隐藏navigationItem左右按钮的方法
- iOS简单代理反向传值
- IOS动画Core Animation详解
- iOS 实现SOAP协议
- iOS开发使用MJRefresh进行刷新
- IOS开发使用YiRefresh进行刷新
- iOS- 详解文本属性Attributes
- iOS使用Charles(青花瓷)抓包并篡改返回数据图文详解
- iOS使用NSMutableAttributedString实现富文本
- storyboard 设置view边框背景
- Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得
- 关于iOS开发的各种证书
- iOS7.0隐藏状态栏(statusBarHidden)