-
前言:
-
開發(fā)中,搭建項(xiàng)目的基本框架辖佣,都會(huì)使用TabbarContrller外加嵌套導(dǎo)航控制器去搭建霹抛,為了能夠快速搭建,節(jié)省開發(fā)時(shí)間卷谈,于是我會(huì)盡量偷懶杯拐,一些簡(jiǎn)單的代碼能盡量能不寫代碼就盡量不寫
TabBar控制器
// LYMMainTabBarVC.m
// 01 - MG的基本框架
// Created by ming on 14/12/1.
// Copyright ? 2014年 ming. All rights reserved.
// 說明:
/**
* 參考代碼:具體類和圖片換一下即可用
*/
#import "LYMMainTabBarVC.h"
#import "LYMNavigationVC.h"
#import "LYMEssenceViewController.h"
#import "LYMNewViewController.h"
#import "LYMFriendViewController.h"
#import "LYMMeViewController.h"
#import "LYMTabBar.h"
@interface LYMMainTabBarVC ()<LYMTabBarDelaegate>
@end
@implementation LYMMainTabBarVC
- (void)viewDidLoad {
[super viewDidLoad];
// 1.當(dāng)系統(tǒng)的Tabbar滿足不了需求的時(shí)候,用自己的TabBar代替系統(tǒng)的TabBar
// [self setValue:[[LYMTabBar alloc] init] forKey:@"tabBar"];
// 2.初始化所有的自控制器
[self setUpAllChildController];
}
#pragma mark ========= initialize ===========
+ (void)initialize{
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabbar-light"]];
NSDictionary *dict = @{
NSForegroundColorAttributeName:[UIColor grayColor]
};
[[UITabBarItem appearance] setTitleTextAttributes:dict forState:UIControlStateNormal];
}
#pragma mark ========= 初始化所有的子控制器 =========
/**
* 初始化所有的子控制器
*/
- (void)setUpAllChildController{
// 1.精華界面
LYMEssenceViewController *essenceCV = [[LYMEssenceViewController alloc] init];
[self setNavOneChildViewController:essenceCV title:@"精華" image:@"tabBar_essence_icon" selImage:@"tabBar_essence_click_icon"];
// 2.新手大廳
LYMNewViewController *newVC = [[LYMNewViewController alloc] init];
newVC.view.backgroundColor = [UIColor redColor];
[self setNavOneChildViewController:newVC title:@"新帖" image:@"tabBar_new_icon"
selImage:@"tabBar_new_click_icon"];
// 3.朋友
LYMFriendViewController *friendVC = [[LYMFriendViewController alloc] init];
[self setNavOneChildViewController:friendVC title:@"關(guān)注" image:@"tabBar_friendTrends_icon" selImage:@"tabBar_friendTrends_click_icon"];
// 4.我
LYMMeViewController *meVC = [[LYMMeViewController alloc] init];
meVC.view.backgroundColor = [UIColor purpleColor];
[self setNavOneChildViewController:meVC title:@"我" image:@"tabBar_me_icon"
selImage:@"tabBar_me_click_icon"];
}
/**
* 初始化一個(gè)子控制器的方法
*/
- (void)setNavOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selImage:(NSString *)selImage {
vc.tabBarItem.title = title;
vc.tabBarItem.image = [UIImage imageNamed:image];
vc.tabBarItem.selectedImage = [UIImage imageNamed:selImage];
[self addChildViewController:[[LYMNavigationVC alloc] initWithRootViewController:vc]];
}
@end
導(dǎo)航控制器
-
主要是重寫push方法和設(shè)置全局導(dǎo)航欄樣式世蔗,以及設(shè)置全局滑動(dòng)返回手勢(shì)
// BSNavigationController.m
// MGNaV
// Created by ming on 14/12/17.
// Copyright ? 2014年 ming. All rights reserved.
#import "BSNavigationController.h"
#define BSBarButtonItemFont [UIFont systemFontOfSize:15]
@interface BSNavigationController ()<UIGestureRecognizerDelegate>
@end
@implementation BSNavigationController
/**
* 為什么在這里設(shè)置端逼??污淋?
* 設(shè)置導(dǎo)航欄全局樣式
* 因?yàn)檫@個(gè)只需要設(shè)置一次顶滩,因?yàn)檫@個(gè)方法程序只會(huì)執(zhí)行一次
*/
+ (void)load{
/// 1.UINavigationBar
UINavigationBar *navBarAppearence = [UINavigationBar appearance];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:17];
[navBarAppearence setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
[navBarAppearence setTitleTextAttributes:dict];
/// 2.UIBarButtonItem
UIBarButtonItem *barItemAppearence = [UIBarButtonItem appearance];
NSMutableDictionary *normalDict = [NSMutableDictionary dictionary];
normalDict[NSForegroundColorAttributeName] = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
normalDict[NSFontAttributeName] = BSBarButtonItemFont;
[barItemAppearence setTitleTextAttributes:normalDict forState:UIControlStateNormal];
NSMutableDictionary *highLDict = [NSMutableDictionary dictionary];
highLDict[NSForegroundColorAttributeName] = BSGlobalBgColor;
highLDict[NSFontAttributeName] = BSBarButtonItemFont;
[barItemAppearence setTitleTextAttributes:highLDict forState:UIControlStateHighlighted];
}
/*
<UIScreenEdgePanGestureRecognizer: 0x7fe260640220; view = <UILayoutContainerView 0x7fe26062bba0>;
target= <(action=handleNavigationTransition:,
target=<_UINavigationInteractiveTransition 0x7fe26063f1a0>)>>
1.UIScreenEdgePanGestureRecognizer 加在導(dǎo)航控制器的view上
2.target:_UINavigationInteractiveTransition(觸發(fā)手勢(shì)的對(duì)象)
3.action: handleNavigationTransition:(這個(gè)方法系統(tǒng)內(nèi)部調(diào)用,不需要自己實(shí)現(xiàn))
觸發(fā)UIScreenEdgePanGestureRecognizer就會(huì)調(diào)用target的handleNavigationTransition:方法
*/
- (void)viewDidLoad {
[super viewDidLoad];
// UIScreenEdgePanGestureRecognizer
// Do any additional setup after loading the view.
// 只要觸發(fā)這個(gè)Pan手勢(shì),就會(huì)調(diào)用self對(duì)象pan方法
// 1.創(chuàng)建全屏手勢(shì)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
// 控制手勢(shì)什么時(shí)候觸發(fā)
pan.delegate = self;
// 全屏滑動(dòng)返回
[self.view addGestureRecognizer:pan];
// 2.禁止邊緣手勢(shì)
self.interactivePopGestureRecognizer.enabled = NO;
// 實(shí)現(xiàn)滑動(dòng)返回功能
// self.interactivePopGestureRecognizer.delegate = self;
// bug:假死:程序一直運(yùn)行,但是界面動(dòng)不了.
// 在根控制器的view,不需要滑動(dòng)返回,
// 全屏滑動(dòng)返回
// 研究下系統(tǒng)自帶的返回手勢(shì)
// NSLog(@"%@",self.interactivePopGestureRecognizer.delegate);
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
// 不是根控制器就實(shí)現(xiàn)滑動(dòng)返回功能
return self.childViewControllers.count != 1;
}
// 攔截控制器的push操作
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
// 判斷是否為根控制器
if (self.childViewControllers.count > 0) { // 非根控制器
// 統(tǒng)一設(shè)置設(shè)置返回按鈕
UIBarButtonItem *backItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"navigationButtonReturn"] highImage:[UIImage imageNamed:@"navigationButtonReturnClick"] norColor:BSColor(66, 66, 66) selColor:BSColor(255, 0, 0) title:@"返回" target:self action:@selector(backClick)];
viewController.navigationItem.leftBarButtonItem = backItem;
// 2.隱藏底部TabBar導(dǎo)航條
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
// 出棧
- (void)backClick{
[self popViewControllerAnimated:YES];
}
@end
附注:當(dāng)系統(tǒng)的Tabbar滿足不了需求寸爆,很多時(shí)候礁鲁,Tabbar界面上都會(huì)有個(gè)發(fā)布(?)按鈕,這時(shí)候就需要自定義Tabbar赁豆,而此文就是為快速解決這個(gè)問題而生
######################### .h文件 #########################
// BSTabBar.h
// MGTabBar
// Created by ming on 14/12/17.
// Copyright ? 2014年 ming. All rights reserved.
#import <UIKit/UIKit.h>
@interface BSTabBar : UITabBar
/** 保存點(diǎn)擊plusButton按鈕之后要執(zhí)行的代碼 */
@property (nonatomic,strong) void(^operationBlock)();
@end
######################### .m文件 #########################
// BSTabBar.m
// MGTabBar
// Created by ming on 14/12/17.
// Copyright ? 2014年 ming. All rights reserved.
#import "BSTabBar.h"
@interface BSTabBar ()
/** 發(fā)布按鈕 */
@property (nonatomic,strong) UIButton *plusButton;
/** 記錄上一次點(diǎn)擊過的按鈕的Tag */
@property (nonatomic,assign) NSInteger previousClicktag;
/** 記錄上一次點(diǎn)擊過的按鈕 */
@property (nonatomic,strong) UIControl *previousClickTaBarButton;
@end
@implementation BSTabBar
/// 蘋果也是懶加載的
// 懶加載plusButton并初始化該按鈕
- (UIButton *)plusButton{
if (!_plusButton) {
_plusButton = [[UIButton alloc] init];
[_plusButton setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
[_plusButton setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
[_plusButton sizeToFit];
[self addSubview:_plusButton];
// 監(jiān)聽按鈕的點(diǎn)擊
[_plusButton addTarget:self action:@selector(plusButtonClick) forControlEvents:UIControlEventTouchUpInside];
}
return _plusButton;
}
// 重新布局TabBar的子控件
- (void)layoutSubviews{
[super layoutSubviews];
CGFloat width = self.width/( self.items.count + 1);
CGFloat height = self.height;
CGFloat x = 0;
CGFloat y = 0;
NSInteger i = 0;
for (UIControl *tabBarButton in self.subviews) {
if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
if (i==2) {
i++;
}
x = i*width;
tabBarButton.frame = CGRectMake(x, y, width, height);
i++;
tabBarButton.tag = i;
[tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
/// 短時(shí)間內(nèi)連續(xù)點(diǎn)擊仅醇,類似于鼠標(biāo)的雙擊事件
// [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchDownRepeat];
}
}
// 計(jì)算plusButton的位置
self.plusButton.center = CGPointMake(self.width*0.5, self.height*0.5);
}
#pragma mark - 操作
// 監(jiān)聽加號(hào)?按鈕的點(diǎn)擊
- (void)plusButtonClick{
if (self.operationBlock) {
self.operationBlock();
}
}
/**
* 重復(fù)點(diǎn)擊TabBar的按鈕,你想做什么事(比如:刷新界面)
*/
- (void)tabBarButtonClick:(UIControl *)tabBarButton{
if (self.previousClicktag == tabBarButton.tag) {
// 告訴其他人(按鈕被重復(fù)點(diǎn)擊了)
[[NSNotificationCenter defaultCenter] postNotificationName:BSTabBarButtonRepeatClickNotification object:nil];
}
self.previousClicktag = tabBarButton.tag;
}
@end
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者