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:1] intValue]>=7) { UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; negativeSpacer.width = -15; 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(0, 0, 60, 44); [leftBtn setImage:image forState:UIControlStateNormal]; [leftBtn addTarget:self action:@selector(leftseaexitAction) forControlEvents:UIControlEventTouchUpInside]; if (GetVesion == 11.0) { leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [leftBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)]; 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(0, 0, 60, 44); _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(0, 0, 0, -20)]; 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(0,0,88,44)]; [rightbBarButton setTitle:itemTitle forState:(UIControlStateNormal)]; [rightbBarButton setTitleColor:kDarkOneColorforState:(UIControlStateNormal)]; rightbBarButton.titleLabel.font = [UIFontsystemFontOfSize:14]; [rightbBarButton addTarget:selfaction:actionforControlEvents:(UIControlEventTouchUpInside)]; if (GetVesion == 11.0) {
rightbBarButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
[rightbBarButton setTitleEdgeInsets:UIEdgeInsetsMake(0,0,0, -20)];
} else
{
//这里适配以前的版本写老方法就可以
}
self.navigationItem.rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:rightbBarButton];
}
//左侧为文字item的情况 - (void)addLeftBarButtonItemWithTitle:(NSString *)itemTitle action:(SEL)action { UIButton *leftbBarButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,44,44)]; [leftbBarButton setTitle:itemTitleforState:(UIControlStateNormal)]; [leftbBarButton setTitleColor:kDarkOneColorforState:(UIControlStateNormal)]; leftbBarButton.titleLabel.font = [UIFontsystemFontOfSize:16]; [leftbBarButton addTarget:selfaction:actionforControlEvents:(UIControlEventTouchUpInside)]; if (GetVesion == 11.0) {
leftbBarButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
[leftbBarButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -5 *kScreenWidth/375.0,0,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(0,0,80,44)]; view.backgroundColor = [UIColorclearColor]; UIButton *firstButton = [UIButtonbuttonWithType:UIButtonTypeCustom]; firstButton.frame = CGRectMake(44, 6, 30, 30); [firstButton setImage:firstImageforState:UIControlStateNormal]; [firstButton addTarget:selfaction:firstActionforControlEvents:UIControlEventTouchUpInside]; if (GetVesion == 11.0) {
firstButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
[firstButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,0, -5 * kScreenWidth/375.0)];
} else
{
//这里适配以前的版本写老方法就可以
}
[view addSubview:firstButton];
UIButton *secondButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
secondButton.frame = CGRectMake(6, 6, 30, 30);
[secondButton setImage:secondImageforState:UIControlStateNormal];
[secondButton addTarget:selfaction:secondActionforControlEvents:UIControlEventTouchUpInside];
if (GetVesion == 11.0) {
secondButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
[secondButton setImageEdgeInsets:UIEdgeInsetsMake(0,0,0, -5 * kScreenWidth/375.0)];
}
[view addSubview:secondButton];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:view];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}
备注:千万别忘了,适配ios11之前的系统,最后在上面的if方法里面要适配以前的版本!
然后在控制器里面,直接 [self 方法]调用即可,如果坐标不合适可以自行处理,如果你的需求是三个四个item按钮,可以仿照上边两个item按钮的方法,自行处理。
本文来自博客园,作者:稻草人11223,转载请注明原文链接:https://www.cnblogs.com/hero11223/p/5300597.html