ios开发之--iOS 11适配:iOS11导航栏返回偏移
UIBarButtonItem 左边间隙过大,解决方案(ios11之前):
调用下面的方法,设置negativeSpacer.width = -15;就可以解决间隙过大的问题:
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:dayOrWeekButton];
self.navigationItem.leftBarButtonItem = leftItem;
[dayOrWeekButton release];
[leftItem release];
if ([[[[UIDevice currentDevice] systemVersion] substringToIndex:] intValue]>=)
{
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -;
self.navigationItem.leftBarButtonItems = @[negativeSpacer, leftItem];
}else{
self.navigationItem.leftBarButtonItem = leftItem;
}
但是ios11以后,导航栏返回按钮偏移20像素,这个时候,上述方法就不行了,但是在之前是可以的,思路是这样的:写一个基类,在基类里面是把系统的导航给设置了下,并声明了几个方法,然后其他的控制器可以直接继承调用,当然也可以直接用一个view来自定义,这样也可以解决!
具体代码如下,判断系统版本号是为了更好的适配所以的机型(因为有好多人不喜欢升级系统):
//左侧按钮
-(void)addLeftBarButtonWithImg:(UIImage *)image
{
UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftBtn.frame = CGRectMake(, , , );
[leftBtn setImage:image forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(leftseaexitAction) forControlEvents:UIControlEventTouchUpInside]; if (GetVesion == 11.0) {
leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[leftBtn setImageEdgeInsets:UIEdgeInsetsMake(, -, , )];
UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn]; self.navigationItem.leftBarButtonItem = leftBarBtnItem;
}else
{
UIBarButtonItem *leftBarBtnItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
self.navigationItem.leftBarButtonItem = leftBarBtnItem;
}
}
//右侧按钮
-(void)addRightBarButtonWithImg:(UIImage *)image r_hidden:(BOOL)r_hidden
{
_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
_rightButton.frame = CGRectMake(, , , );
_rightButton.hidden = r_hidden;
// _rightButton.backgroundColor = [UIColor grayColor];
[_rightButton setImage:image forState:UIControlStateNormal];
[_rightButton addTarget:self action:@selector(clickRightBtn:) forControlEvents:UIControlEventTouchUpInside]; if (GetVesion == 11.0) {
_rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[_rightButton setImageEdgeInsets:UIEdgeInsetsMake(, , , -)];
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc]initWithCustomView:_rightButton];
self.navigationItem.rightBarButtonItem = rightBarBtn;
}else
{
UIBarButtonItem *rightBarBtnItem = [[UIBarButtonItem alloc]initWithCustomView:_rightButton];
self.navigationItem.leftBarButtonItem = rightBarBtnItem;
} }
//右侧为文字item的情况
- (void)addRightBarButtonItemWithTitle:(NSString *)itemTitle action:(SEL)action
{
UIButton *rightbBarButton = [[UIButtonalloc]initWithFrame:CGRectMake(,,,)];
[rightbBarButton setTitle:itemTitle forState:(UIControlStateNormal)];
[rightbBarButton setTitleColor:kDarkOneColorforState:(UIControlStateNormal)];
rightbBarButton.titleLabel.font = [UIFontsystemFontOfSize:];
[rightbBarButton addTarget:selfaction:actionforControlEvents:(UIControlEventTouchUpInside)];
if (GetVesion == 11.0) {
rightbBarButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
[rightbBarButton setTitleEdgeInsets:UIEdgeInsetsMake(,,, -)];
} else
{
//这里适配以前的版本写老方法就可以
}
self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:rightbBarButton];
}
//左侧为文字item的情况
- (void)addLeftBarButtonItemWithTitle:(NSString *)itemTitle action:(SEL)action
{
UIButton *leftbBarButton = [[UIButtonalloc]initWithFrame:CGRectMake(,,,)]; [leftbBarButton setTitle:itemTitleforState:(UIControlStateNormal)]; [leftbBarButton setTitleColor:kDarkOneColorforState:(UIControlStateNormal)]; leftbBarButton.titleLabel.font = [UIFontsystemFontOfSize:]; [leftbBarButton addTarget:selfaction:actionforControlEvents:(UIControlEventTouchUpInside)]; if (GetVesion == 11.0) { leftbBarButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft; [leftbBarButton setTitleEdgeInsets:UIEdgeInsetsMake(, - *kScreenWidth/375.0,,)];
} else
{
//这里适配以前的版本写老方法就可以
} self.navigationItem.leftBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:leftbBarButton]; }
//右侧两个图片item的情况
- (void)addRightTwoBarButtonsWithFirstImage:(UIImage *)firstImage firstAction:(SEL)firstAction secondImage:(UIImage *)secondImage secondAction:(SEL)secondAction
{
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(,,,)];
view.backgroundColor = [UIColorclearColor];
UIButton *firstButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
firstButton.frame = CGRectMake(, , , );
[firstButton setImage:firstImageforState:UIControlStateNormal];
[firstButton addTarget:selfaction:firstActionforControlEvents:UIControlEventTouchUpInside];
if (GetVesion == 11.0) {
firstButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
[firstButton setImageEdgeInsets:UIEdgeInsetsMake(,,, - * kScreenWidth/375.0)];
} else
{
//这里适配以前的版本写老方法就可以
}
[view addSubview:firstButton];
UIButton *secondButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
secondButton.frame = CGRectMake(, , , );
[secondButton setImage:secondImageforState:UIControlStateNormal];
[secondButton addTarget:selfaction:secondActionforControlEvents:UIControlEventTouchUpInside];
if (GetVesion == 11.0) {
secondButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
[secondButton setImageEdgeInsets:UIEdgeInsetsMake(,,, - * kScreenWidth/375.0)];
}
[view addSubview:secondButton];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:view];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}
备注:千万别忘了,适配ios11之前的系统,最后在上面的if方法里面要适配以前的版本!
然后在控制器里面,直接 [self 方法]调用即可,如果坐标不合适可以自行处理,如果你的需求是三个四个item按钮,可以仿照上边两个item按钮的方法,自行处理。
ios开发之--iOS 11适配:iOS11导航栏返回偏移的更多相关文章
- iOS开发-仿大众点评iPad侧边导航栏
昨天其实已经写了一篇侧边栏的文章,不过感觉还不是很清晰,这篇文章算是补充吧,iPad上看了大众点评的侧边栏,基本上百分之九十类似,具体效果可参考下图: 对比昨天主要做了两个修改,一个是图片和文字的显示 ...
- iOS开发笔记13:顶部标签式导航栏及下拉分类菜单
当内容及分类较多时,往往采用顶部标签式导航栏,例如网易新闻客户端的顶部分类导航,最近刚好有这样的应用场景,参考网络上一些demo,实现了这种导航效果,记录一些要点. 效果图(由于视频转GIF掉帧,滑动 ...
- iOS WKWebView 加载进度条、导航栏返回&关闭 (Swift 4)
导航: 1.加载进度条 2.导航栏增加返回.关闭按钮 加载进度条 效果图 代码如下: self.progressView.trackTintColor = UIColor.white self.pro ...
- iOS开发-- 通过runtime kvc 移除导航栏下方的阴影效果线条
网上查了很多, 都是重新绘制, 感觉有点蠢, 恰巧工作有会闲, 就简单的通过runtime遍历了下属性找寻了下私有类和方法, 这里直接贴方法, 找寻过程也发出来, 能看懂的直接就能看懂, 看不太明白的 ...
- iOS - push 或 pop或点击导航栏返回pop指定导航控制器
以前一直有个很疑惑的问题没有搞清楚 关于ios中 viewcontroller的跳转问题,其中有一种方式是采用navigationController pushViewController 的方法,比 ...
- iOS 11 导航栏 item 偏移问题 和 Swift 下 UIButton 设置 title、image 显示问题
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- iOS开发UI篇—多控制器和导航控制器简单介绍
iOS开发UI篇—多控制器和导航控制器简单介绍 一.多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单.当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个vi ...
- iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期
iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期 一.基本过程 新建一个项目,系统默认的主控制器继承自UIViewController,把主控制器两个文件删掉. 在stor ...
- iOS开发小技巧 - runtime适配字体
iOS开发小技巧 - runtime适配字体 版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com 一个iOS开发项目无 ...
随机推荐
- 使用matplot做图--sin图像
# _*_ coding:utf-8 _*_ import numpy as np import matplotlib.pyplot as plt x = np.arange(-5, 5, 0.1) ...
- 来自阿里的 json 解析方案 fastjson
说起Json 解析,有非常多方法,不管是出自Google 的Gson也好,还是来自其它的某某.想必大家都非常熟悉. 今日在github上闲逛.偶遇 一 json 解析库.看起来非常不错,据说是眼下最快 ...
- 【Visual Studio】“诊断工具”窗口不支持当前的调试配置
问题:在运行Debug后,无法使用诊断工具. 解决办法: http://stackoverflow.com/questions/32167640/visual-studio-2015-diagnost ...
- JAVA用POI读取和创建2003和2007版本Excel完美示例
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- fullcalendar案例一<原>
fullcalendar是个很强大的日历控件,可以用它进行排班.排会议.拍任务,很直观,用户体验良好. 看下效果图: #parse("index/head.vm") <lin ...
- pip国内源
pip install -i https://pypi.douban.com/simple pyqrcode
- openfire url get提交 中文乱码问题
原因是它只接受url编码后的中文 如:%E7%BC%96%E7%A0%81%E5%90%8E%E7%9A%84%E4%B8%AD%E6%96%87 会自动转变为:http://127.0.0.1:90 ...
- 逻辑斯特回归(logistic regression)的迭代变权最小平方差算法(IRLS)
参考资料:http://blog.csdn.net/xuanyuansen/article/details/41050507 习题: 数据及代码: https://pan.baidu.com/s/1 ...
- ROS 教程之 navigation :在 catkin 环境下创建costmap layer plugin
在做机器人导航的时候,肯定见到过global_costmap和local_costmap.global_costmap是为了全局路径规划服务的,如从这个房间到那个房间该怎么走.local_costma ...
- ROS 教程之 network:多台计算机之间网络通信(2)
在上一篇文章中我们已经搭建好了两台计算机间通信的条件,但是每次都需要在新的终端里输入一长串export ROS_MASTER_URI之类的.实际弄起来的时候也不方便,因此在本文中,我们更进一步,简化两 ...