OC 下自定義TabBarController(類似微博TabBarItem)

1:創(chuàng)建繼承于UITabBar的類MyTabBar

2:設(shè)置代理實(shí)現(xiàn)UITabBar的代理協(xié)議

@protocol?MyTabBarDelegate


- (void)tabBarDidClickCenterButton:(XPFTabBar *)tabBar;


@end

聲明代理

@property?(nonatomic, weak) id delegate;

3:在.m初始化方法重寫

- (nonnull instancetype)initWithFrame:(CGRect)frame {

? ? if (self = [super initWithFrame:frame]) {

? ? ? ? UIView * backView = [[UIView alloc]init];

? ? ? ? backView.backgroundColor = [UIColor whiteColor];

? ? ? ? backView.layer.cornerRadius = 32.5;

? ? ? ? backView.layer.masksToBounds = YES;

? ? ? ? [self addSubview:backView];

? ? ? ? [backView mas_makeConstraints:^(MASConstraintMaker *make) {

? ? ? ? ? ? make.centerX.equalTo(self.mas_centerX);

? ? ? ? ? ? make.top.equalTo(@0).offset(-15);

? ? ? ? ? ? make.width.equalTo(@65);

? ? ? ? ? ? make.height.equalTo(@65);

? ? ? ? }];

? ? ? ? UIButton *publishButton = [[UIButton alloc] init];

? ? ? ? [publishButton setBackgroundImage:[UIImage imageNamed:@"WALLET"] forState:UIControlStateNormal];

? ? ? ? [publishButton setBackgroundImage:[UIImage imageNamed:@"WALLET"] forState:UIControlStateHighlighted];

? ? ? ? publishButton.layer.cornerRadius = 29;

? ? ? ? publishButton.layer.masksToBounds = YES;

? ? ? ? [backView addSubview:publishButton];

? ? ? ? [publishButton mas_makeConstraints:^(MASConstraintMaker *make) {

? ? ? ? ? ? make.centerX.equalTo(backView.mas_centerX);

? ? ? ? ? ? make.centerY.equalTo(backView.mas_centerY);

? ? ? ? ? ? make.width.equalTo(@58);

? ? ? ? ? ? make.height.equalTo(@58);

? ? ? ? }];

? ? ? ? [publishButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];

? ? }

? ? return self;

}

- (void)publishClick {

? ? [self.delegate tabBarDidClickCenterButton:self];


}

//重新設(shè)置位置

- (void)layoutSubviews

{

? ? [super layoutSubviews];


? ? // 原來的4個(gè)

? ? CGFloat width = self.xpf_width / 5;

? ? int index = 0;

? ? for (UIControl *control in self.subviews) {

? ? ? ? if (![control isKindOfClass:[UIControl class]] || [control isKindOfClass:[UIButton class]]) continue;

? ? ? ? control.xpf_width = width;

? ? ? ? control.xpf_x = index > 1 ? width * (index + 1) : width * index;

? ? ? ? if (index == 1) {

? ? ? ? }

? ? ? ? index++;

? ? }


}

4:創(chuàng)建繼承于UITabBarController的MyTabBarController

//重寫initialze,定義TabBar

+ (void)initialize {

? ? UITabBarItem *appearance = [UITabBarItem appearance];

? ? NSMutableDictionary *attrs = [NSMutableDictionary dictionary];

? ? attrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];

? ? [appearance setTitleTextAttributes:attrs forState:UIControlStateSelected];

//? ? [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"BG_image"]];? //tabbar-light

? ? [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];


}

- (void)viewDidLoad {


? ? // 替換tabbar

? ? MyTabBar *tabBar = [[MyTabBar alloc] init];

? ? tabBar.adDelegate = self;

? ? [self setValue:tabBar forKeyPath:@"tabBar"];

? ? // 添加

? ? [self setupChildViewControllers];

? ? //去掉頂部的線的效果,即陰影圖片和背景圖片一樣時(shí)

? ? CGRect rect = CGRectMake(0, 0, MAINSCREEN.size.width, 1);

? ? UIGraphicsBeginImageContext(rect.size);

? ? CGContextRef context = UIGraphicsGetCurrentContext();

? ? CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);

? ? CGContextFillRect(context, rect);

? ? UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

? ? UIGraphicsEndImageContext();

? ? [self.tabBar setBackgroundImage:img];

? ? [self.tabBar setShadowImage:img];

}

- (void)setupChildViewControllers {

? ? [self addAppearance];


}


#pragma mark - <添加一個(gè)子控制器>

- (void)setUpOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selImage:(NSString *)selImage {

? ? vc.navigationItem.title = title;

? ? vc.tabBarItem.title = title;

? ? vc.tabBarItem.image = [UIImage imageNamed:image];

? ? vc.tabBarItem.selectedImage = [[UIImage imageNamed:selImage] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

? ? [self addChildViewController:[[NavigationController alloc] initWithRootViewController:vc]];

}


#pragma mark - 設(shè)置顏色

- (void)addAppearance {

? ? [UINavigationBar appearance];

? ? NSMutableDictionary *attrs = [NSMutableDictionary dictionary];

? ? attrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];

? ? // 未選中的顏色

? ? attrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:138/255.0 green:138/255.0 blue:138/255.0 alpha:1.0];

? ? NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];

? ? selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];

? ? // 點(diǎn)擊后的顏色

? ? selectedAttrs[NSForegroundColorAttributeName] = [UIColorChange colorwithHexString:MAINCOLOR];

? ? UITabBarItem *item = [UITabBarItem appearance];

? ? [item setTitleTextAttributes:attrs forState:UIControlStateNormal];

? ? [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

}



#pragma mark - TabBarDelegate點(diǎn)擊中間按鈕觸發(fā)的方法

- (void)tabBarDidClickCenterButton:(XPFTabBar *)tabBar {

? ? self.wvc = [[WalletViewController alloc]init];

? ? self.wvc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];

? ? [self.wvc.closeButton addTarget:self action:@selector(closeClick:) forControlEvents:UIControlEventTouchUpInside];

? ? [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self.wvc.view];

}

- (void)closeClick:(UIButton *)sender{

? ? [self.wvc.view removeFromSuperview];

? ? self.wvc = nil;

}

// 手勢(shì)點(diǎn)擊事件

- (void)touch {

? ? [self remove_adVeiw];

}


/** 刪除 view */

- (void)remove_adVeiw {

? ? [adView removeFromSuperview];

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末晴弃,一起剝皮案震驚了整個(gè)濱河市奸鸯,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌年碘,老刑警劉巖橘茉,帶你破解...
    沈念sama閱讀 218,525評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件工腋,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡畅卓,警方通過查閱死者的電腦和手機(jī)擅腰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來翁潘,“玉大人趁冈,你說我怎么就攤上這事“萋恚” “怎么了渗勘?”我有些...
    開封第一講書人閱讀 164,862評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)俩莽。 經(jīng)常有香客問我旺坠,道長(zhǎng),這世上最難降的妖魔是什么扮超? 我笑而不...
    開封第一講書人閱讀 58,728評(píng)論 1 294
  • 正文 為了忘掉前任取刃,我火速辦了婚禮蹋肮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘璧疗。我一直安慰自己坯辩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評(píng)論 6 392
  • 文/花漫 我一把揭開白布崩侠。 她就那樣靜靜地躺著漆魔,像睡著了一般。 火紅的嫁衣襯著肌膚如雪啦膜。 梳的紋絲不亂的頭發(fā)上有送,一...
    開封第一講書人閱讀 51,590評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音僧家,去河邊找鬼雀摘。 笑死,一個(gè)胖子當(dāng)著我的面吹牛八拱,可吹牛的內(nèi)容都是我干的阵赠。 我是一名探鬼主播,決...
    沈念sama閱讀 40,330評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼肌稻,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼清蚀!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起爹谭,我...
    開封第一講書人閱讀 39,244評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤枷邪,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后诺凡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體东揣,經(jīng)...
    沈念sama閱讀 45,693評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評(píng)論 3 336
  • 正文 我和宋清朗相戀三年腹泌,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了嘶卧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,001評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡凉袱,死狀恐怖芥吟,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情专甩,我是刑警寧澤钟鸵,帶...
    沈念sama閱讀 35,723評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站涤躲,受9級(jí)特大地震影響棺耍,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜篓叶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評(píng)論 3 330
  • 文/蒙蒙 一烈掠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧缸托,春花似錦左敌、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至佩抹,卻和暖如春叼风,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背棍苹。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工无宿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人枢里。 一個(gè)月前我還...
    沈念sama閱讀 48,191評(píng)論 3 370
  • 正文 我出身青樓孽鸡,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親栏豺。 傳聞我的和親對(duì)象是個(gè)殘疾皇子彬碱,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • (一)Masonry介紹 Masonry是一個(gè)輕量級(jí)的布局框架 擁有自己的描述語法 采用更優(yōu)雅的鏈?zhǔn)秸Z法封裝自動(dòng)布...
    木易林1閱讀 2,338評(píng)論 0 3
  • Masonry是一個(gè)輕量級(jí)的布局框架,擁有自己的描述語法奥洼,采用更優(yōu)雅的鏈?zhǔn)秸Z法封裝自動(dòng)布局巷疼,簡(jiǎn)潔明了并具有高可讀性...
    3dcc6cf93bb5閱讀 1,768評(píng)論 0 1
  • iOS_autoLayout_Masonry 概述 Masonry是一個(gè)輕量級(jí)的布局框架與更好的包裝AutoLay...
    指尖的跳動(dòng)閱讀 1,163評(píng)論 1 4
  • *Masonry有哪些屬性 @property (nonatomic, strong, readonly) MAS...
    Albert新榮閱讀 293評(píng)論 0 0
  • 關(guān)于tabBar這個(gè)控件,相信大家都不太陌生,基本上每個(gè)app都會(huì)用的到tab.但是,使用的情況卻不一定,根...
    深白色的熊閱讀 2,226評(píng)論 3 12