作者 sundays http://www.cnblogs.com/sundaysgarden/
OC中UITabbar的适配[iphoneX和Ipad适配]
自定可以UITabar
自定义UITabar头文件
#import <UIKit/UIKit.h>
@interface MCTabBar : UITabBar
@property (nonatomic, strong) UIButton *centerBtn; //中间按钮
@end
自定义UITabar m文件
#import "GBArcView.h"
//#import ""
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define GBVIEWSCALL 1.2
#define CENTERBUTTONSCALL 1.1
@interface MCTabBar ()
@property (nonatomic,strong) GBArcView *gbview; //半圆View
@property(assign,nonatomic)int index;//UITabBar子view的索引
@end
@implementation MCTabBar
//重新初始化方法,从stroyboard 中加载,会调用
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
self.backgroundColor=[UIColor whiteColor];
self.clipsToBounds=NO;//不裁剪子控件
self.selectedItem=0;//初始化索引
//设置tabBaritem 的文字颜色
// [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:RGB_COLOR(74, 74, 74), UITextAttributeTextColor, nil] forState:UIControlStateNormal];
//
// [[UITabBarItem appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:RGB_COLOR(0, 147, 197),UITextAttributeTextColor, nil]forState:UIControlStateSelected];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]){
[self initView];
}
return self;
}
- (void)initView{
[self addSubview:_centerBtn];
[self insertSubview:_gbview belowSubview:_centerBtn];
}
//处理超出区域点击无效的问题
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if (self.hidden){
return [super hitTest:point withEvent:event];
}else {
//转换坐标
CGPoint tempPoint = [self.centerBtn convertPoint:point fromView:self];
//判断点击的点是否在按钮区域内
if (CGRectContainsPoint(self.centerBtn.bounds, tempPoint)){
_centerBtn.selected = YES;
//返回按钮
return _centerBtn;
}else {
_centerBtn.selected = NO;
// __weak __typeof(&*self)weakSelf =self;
return [super hitTest:point withEvent:event];
}
}
}
//绘制横线
- (void)drawRect:(CGRect)rect {
//中间的按钮宽度是UItabBar的高度,其他按钮的宽度就是,(self.width-self.height)/4.0
CGFloat buttonW = (self.width-self.height)/4.0;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:0.8].CGColor);
CGContextSetLineWidth(context, SINGLE_LINE_WIDTH + 2.0f);
// CGContextSetLineWidth(context, 5);
CGContextBeginPath(context);
CGFloat lineMargin =0;
//1PX线,像素偏移
CGFloat pixelAdjustOffset = 0;
if (((int)(1 * [UIScreen mainScreen].scale) + 1) % 2 == 0) {
pixelAdjustOffset = SINGLE_LINE_ADJUST_OFFSET;
}
CGFloat yPos = lineMargin - pixelAdjustOffset;
//第一段线
CGContextMoveToPoint(context, 0, yPos);
CGContextAddLineToPoint(context, buttonW*2+SINGLE_LINE_WIDTH*2, yPos);
CGContextStrokePath(context);
//第二段线
CGContextMoveToPoint(context, buttonW*2+self.frame.size.height-SINGLE_LINE_WIDTH*2, yPos);
CGContextAddLineToPoint(context, self.bounds.size.width, yPos);
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:0.8].CGColor);
CGContextStrokePath(context);
}
//自定义按钮的懒加载
-(UIButton *)centerBtn{
if(!_centerBtn){
_centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// 设定button大小为适应图片
UIImage *normalImage = [UIImage imageNamed:@"3_gray"];
_centerBtn.frame = CGRectMake(0, 0, normalImage.size.width, normalImage.size.height);
[_centerBtn setImage:normalImage forState:UIControlStateNormal];
UIImage *selectImage = [UIImage imageNamed:@"3_hover"];
[_centerBtn setImage:selectImage forState:UIControlStateSelected];
//去除选择时高亮
_centerBtn.adjustsImageWhenHighlighted = NO;
//根据图片调整button的位置(图片中心在tabbar的中间最上部,这个时候由于按钮是有一部分超出tabbar的,所以点击无效,要进行处理)
_centerBtn.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - normalImage.size.width)/2.0, - normalImage.size.height/2.0 + 8, normalImage.size.width, normalImage.size.height);
_centerBtn.transform = CGAffineTransformMakeScale(CENTERBUTTONSCALL, CENTERBUTTONSCALL);
}
return _centerBtn;
}
////自定义半圆View的懒加载
-(UIView *)gbview{
if(!_gbview){
CGFloat buttonW = self.centerBtn.width;
GBArcView *gbview = [[GBArcView alloc]initWithFrame:CGRectMake(0,0,buttonW *GBVIEWSCALL,buttonW*GBVIEWSCALL)];
gbview.backgroundColor=[UIColor whiteColor];
gbview.layer.masksToBounds=YES;
gbview.layer.cornerRadius=buttonW*GBVIEWSCALL*0.5f;
gbview.center = _centerBtn.center;
gbview.transform = CGAffineTransformMakeScale(GBVIEWSCALL, GBVIEWSCALL);
_gbview = gbview;
}
return _gbview;
}
-(void)setHidden:(BOOL)hidden{
[super setHidden:hidden];
//手动设置UITabBar 隐藏时,我们要将自定义的按钮和背景隐藏
[self.gbview setHidden:hidden];
[self.centerBtn setHidden:hidden];
}
//******核心部分******
//当配置 hidesBottomBarWhenPushed 的viewController ,隐藏UITabBar时,会改变其frame,就是将UITabBar 的Y值设为屏幕最大的y值,就不可见。我们重写这个方法,判断当frame的y小于屏幕的高度 ,那么UITabBar就是被隐藏了,这时候我们将自定的控件隐藏。相反的,我们就显示我们的自定义控件。
-(void)setFrame:(CGRect)frame{
// if (self.superview &&CGRectGetMaxY(self.superview.bounds) !=CGRectGetMaxY(frame)) {
// frame.origin.y =CGRectGetHeight(self.superview.bounds) -CGRectGetHeight(frame);
// }
[super setFrame:frame];
if(frame.origin.y>=[UIScreen mainScreen].bounds.size.height){
[self.gbview setHidden:YES];
[self.centerBtn setHidden:YES];
}else{
[self.gbview setHidden:NO];
[self.centerBtn setHidden:NO];
}
}
//适配ipad
//重写traitCollection方法,使UITabbar保持图为垂直排列,防止在ipad11出现水平排列
- (UITraitCollection *)traitCollection {
if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
return [UITraitCollection traitCollectionWithVerticalSizeClass:UIUserInterfaceSizeClassCompact];
}
return [super traitCollection];
}
@end
在UITabBarController.m文件中设置适配iPhoneX
//利用KVC 将自己的tabbar赋给系统tabBar
ViewDidLoad方法添加如下代码
//利用kvc将自定义的文件copy给系统
[self setValue:_mcTabbar forKeyPath:@"tabBar"];
给系统的[自定义]的UITabBar添加一个View设置UITabBar的高度
[self setupTabBar];
- (void)setupTabBar{
//删除现有的tabBar
CGRect rect = self.tabBar.frame;
[self.tabBar removeFromSuperview]; //移除TabBarController自带的下部的条
// [self.tabBarController.tabBar removeFromSuperview];
rect.size.height = 49;
rect.origin.y = [UIScreen mainScreen].bounds.size.height - 49;
UIView *myView = [[UIView alloc] init];
myView.frame = rect;
myView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myView];
}
自定义UItabBar中引用的文件
GBArcView.h
#define SINGLE_LINE_WIDTH (1 / [UIScreen mainScreen].scale)
#define SINGLE_LINE_ADJUST_OFFSET ((1 / [UIScreen mainScreen].scale) / 2)
@interface GBArcView : UIView
@end
GBArch.m
#import <QuartzCore/QuartzCore.h>
#import "Math.h"
#import <CoreGraphics/CoreGraphics.h>
@implementation GBArcView
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *color1 = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:0.8];
CGContextSetStrokeColorWithColor(context, color1.CGColor);
CGContextSetLineWidth(context, SINGLE_LINE_WIDTH + 0.5f);
CGContextBeginPath(context);
// CGFloat lineMargin =self.frame.size.width*0.5f;
//1px线,偏移像素点
CGFloat pixelAdjustOffset = 0;
if (((int)(1 * [UIScreen mainScreen].scale) + 1) % 2 == 0) {
pixelAdjustOffset = SINGLE_LINE_ADJUST_OFFSET;
}
CGFloat yPos = self.frame.size.width*0.5f - pixelAdjustOffset;
CGFloat xPos = self.frame.size.width*0.5f - pixelAdjustOffset - 4.0f;
CGContextAddArc(context, xPos, yPos, self.frame.size.width*0.5 - 1.f , M_PI*1.08, M_PI*1.95, 0);
CGContextDrawPath(context, kCGPathStroke);
// CGContextStrokePath(context);
}
@end
适配IPhoneX