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开发项目无 ...
随机推荐
- xslt循环转换子元素
转换源xml <keywords class="array"> <e type="string">e1</e> <e ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 【C++程序员学 python】python 之变量
既然学过C++,那么就应该知道变量是什么,常量是什么. python 相比于C++,在使用变量之前不用先声明. 而是直接使用,python 会根据你的变量自动识别其类型. 假如a = 123 那么a ...
- 【Unity】计时器
看了好些方法,终于找到一个超级好用的计时器,立马转载马住了! http://www.gimoo.net/t/1602/56bfcc8a26757.html 运行效果如下: 思路:记录当前游戏时间然后进 ...
- C语言 · 核桃的数量
历届试题 核桃的数量 时间限制:1.0s 内存限制:256.0MB 锦囊1 最小公倍数. 锦囊2 答案是a, b, c的最小公倍数. 问题描述 小张是软件项目经理,他带领3个 ...
- Spring 网路搜集的情报
Spring Validate http://haohaoxuexi.iteye.com/blog/1812584
- 跟我学TCP/IP系列
最近在微信公众号“java与Android开发专栏”,看到系列文章“跟我学TCP/IP系列”,共7章,文章很赞. 系列文章在CSDN上也有分发,下列出地址以备以后查看(版权问题不转载内容). http ...
- Mac升级yosemite后无法登陆问题
Mac升级yosemite后无法登陆问题 今天心血来潮准备玩玩最新的苹果系统10.10,代号是yosemite.去官网申请了beta版的測试资格,然后在app store下载了一晚上得 ...
- Wordpress搭建社交型小游戏网站10大步骤
http://www.aliyun.com/zixun/content/2_8_196141.html ———————————————————————————————————————————————— ...
- laravel 5.1 性能优化对比 - 框架提供的方法
写了一个项目发现性能不如人意. 于是便测试下, 看下性能瓶颈在什么地方. 使用 ab -n 20 http://www.lartest.com/ 软件环境: OS : windows 8.1 CPU: ...