iOS自定義UITabBar中間按鈕
iOS自定義UITabBar中間按鈕的具體代碼,供大家參考,具體內(nèi)容如下

自定義YLTbaBar繼承自UITabBar
YLTbaBar.h
//
// YLTabBar.h
// 自定義tabbar
//
// Created by nyl on 2018/10/15.
// Copyright © 2018年 nieyinlong. All rights reserved.
//
#import <UIKit/UIKit.h>
//tab頁(yè)面?zhèn)€數(shù)
typedef NS_ENUM(NSInteger, kTbaBarItemUIType) {
kTbaBarItemUIType_Three = 3,//底部3個(gè)選項(xiàng)
kTbaBarItemUIType_Five = 5,//底部5個(gè)選項(xiàng)
};
@class YLTabBar;
@protocol YLTabBarDelegate <NSObject>
-(void)tabBar:(YLTabBar *)tabBar clickCenterButton:(UIButton *)sender;
@end
@interface YLTabBar : UITabBar
@property (nonatomic, weak) id<YLTabBarDelegate> tabDelegate;
@property (nonatomic, strong) NSString *centerBtnTitle;
@property (nonatomic, strong) NSString *centerBtnIcon;
+ (instancetype)instanceCustomTabBarWithType:(kTbaBarItemUIType)type;
@end
YLTbaBar.m
//
// YLTabBar.m
// 自定義tabbar
//
// Created by nyl on 2018/10/15.
// Copyright © 2018年 nieyinlong. All rights reserved.
//
#import "YLTabBar.h"
@interface YLTabBar()
@property(nonatomic, strong) UIButton *centerButton;
@property(nonatomic, strong) UILabel *centerTitle;
@property (nonatomic,assign) kTbaBarItemUIType type;
@end
@implementation YLTabBar
+(instancetype)instanceCustomTabBarWithType:(kTbaBarItemUIType)type{
YLTabBar *tabBar = [[YLTabBar alloc] init];
tabBar.type = type;
return tabBar;
}
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.translucent = NO;
UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.centerButton = plusBtn;
[plusBtn addTarget:self action:@selector(plusBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:plusBtn];
UILabel *lblTitle = [[UILabel alloc] init];
self.centerTitle = lblTitle;
lblTitle.font = [UIFont systemFontOfSize:10];
lblTitle.textColor = [UIColor blackColor];
lblTitle.textAlignment = NSTextAlignmentCenter;
[self addSubview:lblTitle];
}
return self;
}
-(void)plusBtnDidClick{
if (self.tabDelegate && [self.tabDelegate respondsToSelector:@selector(tabBar:clickCenterButton:)]) {
[self.tabDelegate tabBar:self clickCenterButton:self.centerButton];
}
}
// 調(diào)整子視圖的布局
-(void)layoutSubviews{
[super layoutSubviews];
CGFloat width = self.frame.size.width/self.type;
Class class = NSClassFromString(@"UITabBarButton");
for (UIView *view in self.subviews) {
if ([view isEqual:self.centerTitle]) {//self.centerButton
view.frame = CGRectMake(0, 0, width, 15);
view.center = CGPointMake(self.frame.size.width/2, self.frame.size.height - view.frame.size.height + 8);
}else if ([view isEqual:self.centerButton]) {//self.centerButton
view.frame = CGRectMake(0, 0, width, self.frame.size.height);
[view sizeToFit];
view.center = CGPointMake(self.frame.size.width/2, 10);
}else if ([view isKindOfClass:class]){//system button
CGRect frame = view.frame;
int indexFromOrign = view.frame.origin.x/width;//防止UIView *view in self.subviews 獲取到的不是有序的
if (indexFromOrign >= (self.type - 1) / 2) {
indexFromOrign++;
}
CGFloat x = indexFromOrign * width;
//如果是系統(tǒng)的UITabBarButton,那么就調(diào)整子控件位置,空出中間位置
view.frame = CGRectMake(x, view.frame.origin.y, width, frame.size.height);
//調(diào)整badge postion
for (UIView *badgeView in view.subviews){
NSString *className = NSStringFromClass([badgeView class]);
// Looking for _UIBadgeView
if ([className rangeOfString:@"BadgeView"].location != NSNotFound){
badgeView.layer.transform = CATransform3DIdentity;
badgeView.layer.transform = CATransform3DMakeTranslation(-17.0, 1.0, 1.0);
break;
}
}
}
}
}
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
//這一個(gè)判斷是關(guān)鍵,不判斷的話push到其他頁(yè)面,點(diǎn)擊發(fā)布按鈕的位置也是會(huì)有反應(yīng)的,這樣就不好了
//self.isHidden == NO 說(shuō)明當(dāng)前頁(yè)面是有tabbar的,那么肯定是在導(dǎo)航控制器的根控制器頁(yè)面
//在導(dǎo)航控制器根控制器頁(yè)面,那么我們就需要判斷手指點(diǎn)擊的位置是否在發(fā)布按鈕身上
//是的話讓發(fā)布按鈕自己處理點(diǎn)擊事件,不是的話讓系統(tǒng)去處理點(diǎn)擊事件就可以了
if (self.isHidden == NO) {
//將當(dāng)前tabbar的觸摸點(diǎn)轉(zhuǎn)換坐標(biāo)系,轉(zhuǎn)換到發(fā)布按鈕的身上,生成一個(gè)新的點(diǎn)
CGPoint newP = [self convertPoint:point toView:self.centerButton];
//判斷如果這個(gè)新的點(diǎn)是在發(fā)布按鈕身上,那么處理點(diǎn)擊事件最合適的view就是發(fā)布按鈕
if ( [self.centerButton pointInside:newP withEvent:event]) {
return self.centerButton;
}else{//如果點(diǎn)不在發(fā)布按鈕身上,直接讓系統(tǒng)處理就可以了
return [super hitTest:point withEvent:event];
}
}
else {//tabbar隱藏了,那么說(shuō)明已經(jīng)push到其他的頁(yè)面了,這個(gè)時(shí)候還是讓系統(tǒng)去判斷最合適的view處理就好了
return [super hitTest:point withEvent:event];
}
}
-(void)setCenterBtnIcon:(NSString *)centerBtnIcon{
_centerBtnIcon = centerBtnIcon;
[self.centerButton setBackgroundImage:[UIImage imageNamed:self.centerBtnIcon] forState:UIControlStateNormal];
[self.centerButton setBackgroundImage:[UIImage imageNamed:self.centerBtnIcon] forState:UIControlStateHighlighted];
}
-(void)setCenterBtnTitle:(NSString *)centerBtnTitle{
_centerBtnTitle = centerBtnTitle;
self.centerTitle.text = centerBtnTitle;
}
@end
在UITabBarController中使用
// viewDidLoda中, KVO形式添加
[self setValue:self.ylTabBar forKey:@"tabBar"];
- (YLTabBar *)ylTabBar {
if (!_ylTabBar) {
_ylTabBar = [YLTabBar instanceCustomTabBarWithType:kTbaBarItemUIType_Five];
_ylTabBar.centerBtnIcon = @"centerIcon";
_ylTabBar.tabDelegate = self;
}
return _ylTabBar;
}
YLTabBarDelegate
-(void)tabBar:(YLTabBar *)tabBar clickCenterButton:(UIButton *)sender{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"點(diǎn)擊了中間按鈕" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
// TODO
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS 開(kāi)發(fā)之ios視頻截屏的實(shí)現(xiàn)代碼
這篇文章主要介紹了IOS 開(kāi)發(fā)之ios視頻截屏的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-07-07
iOS實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
詳解iOS 驗(yàn)證碼輸入的實(shí)現(xiàn)思路
這篇文章主要介紹了iOS 驗(yàn)證碼輸入一種實(shí)現(xiàn)思路,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
iOS開(kāi)發(fā)一個(gè)好看的ActionSheet
本篇文章通過(guò)代碼分享和圖文形式教給大家用IOS寫(xiě)一個(gè)好看的ActionSheet過(guò)程以及注意事項(xiàng),需要的朋友參考下吧。2018-01-01
iOS中設(shè)置清除緩存功能的實(shí)現(xiàn)方法
清除緩存基本上都是在設(shè)置界面的某一個(gè)Cell,于是我們可以把清除緩存封裝在某一個(gè)自定義Cell中,現(xiàn)在位大家介紹一種最基礎(chǔ)的清除緩存的方法,感興趣的朋友一起看看吧2017-07-07
touchesBegan: withEvent: 不執(zhí)行解決
這篇文章主要介紹了touchesBegan: withEvent: 不執(zhí)行解決的相關(guān)資料,需要的朋友可以參考下2016-12-12
iOS基于UITableView實(shí)現(xiàn)多層展開(kāi)與收起
這篇文章主要為大家詳細(xì)介紹了iOS基于UITableView實(shí)現(xiàn)多層展開(kāi)與收起的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
iOS開(kāi)發(fā)底層探索界面優(yōu)化示例詳解
這篇文章主要為大家介紹了iOS開(kāi)發(fā)底層探索界面優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
iOS APP中保存圖片到相冊(cè)時(shí)崩潰的解決方法
下面小編就為大家分享一篇iOS APP中保存圖片到相冊(cè)時(shí)崩潰的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
iOS開(kāi)發(fā)實(shí)現(xiàn)下載器的基本功能(1)
這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)發(fā)實(shí)現(xiàn)下載器基本功能的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07

