標籤:and rcv elf nat from tabbar ide 轉換 self
作者 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
定義UITabar的分類實現適配
Swift中讓TabBarItem的表徵圖和檔案在IPad中仍然是上下排列
http://www.cnblogs.com/sundaysgarden/articles/9044340.html
OC和Swift中的UITabBar和UINaviGationBar的適配 [UITabbar在IPad中的適配]