Demo:
AppDelegate.m文件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
/*以下代碼皆是位於此*/
return YES;
}
其中用到的相關代碼:
#import "MMDrawerController.h"//抽屜框架
#define TBItem(title, img, sImg) [[UITabBarItem alloc] initWithTitle:title image:[[UIImage imageNamed:img] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:sImg] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]]
/*
設置UIImage的渲染模式:UIImage.renderingMode
着色(Tint Color)是iOS7界面中的一個.設置UIImage的渲染模式:UIImage.renderingMode重大改變,你可以設置一個UIImage在渲染時是否使用當前視圖的Tint Color。UIImage新增了一個只讀屬性:renderingMode,對應的還有一個新增方法:imageWithRenderingMode:,它使用UIImageRenderingMode枚舉值來設置圖片的renderingMode屬性。該枚舉中包含下列值:
1.UIImageRenderingModeAutomatic // 根據圖片的使用環境和所處的繪圖上下文自動調整渲染模式。
2.UIImageRenderingModeAlwaysOriginal // 始終繪製圖片原始狀態,不使用Tint Color。
3.UIImageRenderingModeAlwaysTemplate // 始終根據Tint Color繪製圖片,忽略圖片的顏色信息。
4.renderingMode屬性的默認值是UIImageRenderingModeAutomatic,即UIImage是否使用Tint Color取決於它顯示的位置
*/
- (void)tabBarControllers{
firstCtrl = [[ViewController alloc]init];
//自定義標籤欄圖片
firstCtrl.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"任務" image:[UIImage imageNamed:@"Task"] selectedImage:[UIImage imageNamed:@"Task_Normal"]];
//重點關注控制器的層次關係,只有ViewController內的firstCtrl 及從firstCtrl跳轉到的頁面 具有導航控制器,
SecondCtrl = [[SecondViewController alloc]init];
//標籤欄
SecondCtrl.tabBarItem = TBItem(@"統計", @"Stat_Normal", @"Stat");
ThirdCtrl = [[ThirdViewController alloc]init];
// ThirdCtrl.tabBarItem.title = @"5";
// ThirdCtrl.tabBarItem.image = [UIImage imageNamed:@"Task_Normal"]
// ;
// ThirdCtrl.tabBarItem.selectedImage = [UIImage imageNamed:@"Task"];
// ThirdCtrl.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:2];
ThirdCtrl.title = @"4";
//官方標籤欄圖片樣式UITabBarSystemItemXXX
/*
UITabBarSystemItemMore,//更多圖標
UITabBarSystemItemFavorites,//最愛圖標
UITabBarSystemItemFeatured,//特徵圖標
UITabBarSystemItemTopRated,//高級圖標
UITabBarSystemItemRecents,//最近圖標
UITabBarSystemItemContacts,//聯繫人圖標
UITabBarSystemItemHistory,//歷史圖標
UITabBarSystemItemBookmarks,//圖書圖標
UITabBarSystemItemSearch,//查找圖標
UITabBarSystemItemDownloads,//下載圖標
UITabBarSystemItemMostRecent,//記錄圖標
UITabBarSystemItemMostViewed,//全部查看圖標
*/
nextfirstCtrl = [[FouthViewController alloc]init];
nextfirstCtrl.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
}
//⻚面是否切換,NO爲不切換
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
if ([tabBarController.viewControllers lastObject] == viewController) {//地址相同,則肯定是同一個
SixthViewController *loginCtrl = [SixthViewController new];
[tabBarController presentViewController:loginCtrl animated:YES completion:nil];//新頁面從下往上出現
return NO;
}
return YES;
}
1.只加導航條
#pragma mark - 只加導航條
#if 1
//設置導航條顏色
[[UINavigationBar appearance]setBarTintColor:[UIColor colorWithRed:80.0/255 green:227.0/255 blue:194.0/255 alpha:1.0]];
//設置導航條上按鈕的文字或圖標的顏色
[[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];
//設置導航條上的背景圖片
// [[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"Snip20150916_1.png"] forBarMetrics:UIBarMetricsDefault];
ViewController * VC = [[ViewController alloc] init];
//創建導航控制器,並使用根視圖控制器初始化
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:VC];
self.window.rootViewController = navCtrl;
#endif
2.加導航條和標籤欄
#pragma mark - 加導航條和標籤欄
#if 1
//設置導航條上按鈕的文字或圖標的顏色
[[UINavigationBar appearance]setTintColor:[UIColor blueColor]];
//設置標籤欄上文字的顏色
[[UITabBar appearance]setTintColor:[UIColor greenColor]];
[self tabBarControllers];
#pragma mark - --每個標籤各有一個NavigationController
#if 1 //每個標籤各有一個NavigationController
UINavigationController *controller1 = [[UINavigationController alloc] initWithRootViewController:firstCtrl];
UINavigationController *controller2 = [[UINavigationController alloc] initWithRootViewController:SecondCtrl];
UINavigationController *controller3 = [[UINavigationController alloc] initWithRootViewController:ThirdCtrl];
UINavigationController *controller4 = [[UINavigationController alloc] initWithRootViewController:nextfirstCtrl];
controller1.tabBarItem.title = @"自定義圖片刷新";//標籤欄標題
// controller1.navigationItem.title = @"自定義動態圖片上拉下拉刷新";//單單導航條標題
controller2.tabBarItem.title = @"自定義文字刷新";
controller3.tabBarItem.title = @"自定義刷新控件";
controller4.tabBarItem.title = @"無";//跳轉到無導航條默認刷新
UITabBarController *tabBarCtrl = [[UITabBarController alloc]init];
tabBarCtrl.delegate = self;//注意添加協議<UITabBarControllerDelegate>
// NSArray *viewControllers = [NSArray arrayWithObjects:controller1, controller2, controller3, controller4, nil];
tabBarCtrl.viewControllers = @[controller1,controller2,controller3,controller4];
self.window.rootViewController = tabBarCtrl;
#endif
#pragma mark - --所有標籤共用一個NavigationController
#if 0 //所有標籤共用一個NavigationController
UITabBarController *tabBarCtrl = [[UITabBarController alloc]init];
tabBarCtrl.delegate = self;//注意添加協議<UITabBarControllerDelegate>
tabBarCtrl.viewControllers = @[firstCtrl,SecondCtrl,ThirdCtrl,nextfirstCtrl];
//只有標籤欄
// self.window.rootViewController = tabBarCtrl;//只有標籤欄
//有導航條和標籤欄
//重點關注控制器的層次關係,具有導航控制器,
//創建導航控制器,並使用根視圖控制器初始化
UINavigationController *allNavCtrl = [[UINavigationController alloc]initWithRootViewController:tabBarCtrl];
tabBarCtrl.title = @"所有tabBar的標題";
self.window.rootViewController = allNavCtrl;
#endif
#endif
3.加導航條和標籤欄和抽屜
#pragma mark - 加導航條和標籤欄和抽屜
#if 1
[self tabBarControllers];
UITabBarController *tabBarCtrl = [[UITabBarController alloc]init];
tabBarCtrl.viewControllers = @[firstCtrl,SecondCtrl,ThirdCtrl,nextfirstCtrl];
LeftViewController *leftCtrl = [[LeftViewController alloc]init];
//只加標籤欄和抽屜
// MMDrawerController *drawerCtrl = [[MMDrawerController alloc]initWithCenterViewController:tabBarCtrl leftDrawerViewController:leftCtrl];
//創建導航控制器,並使用根視圖控制器初始化
UINavigationController *navCtrl = [[UINavigationController alloc]initWithRootViewController:tabBarCtrl];
tabBarCtrl.title = @"所有tabBar的標題";
//加導航條和標籤欄和抽屜
MMDrawerController *drawerCtrl = [[MMDrawerController alloc]initWithCenterViewController:navCtrl leftDrawerViewController:leftCtrl];
//支持打開抽屜的手勢類型
// drawerCtrl.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
//MMOpenDrawerGestureModePanningCenterView:打開抽屜姿態模式平移中心視圖
//MMOpenDrawerGestureModePanningNavigationBar:打開抽屜姿態模式平移導航欄
drawerCtrl.openDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView |MMOpenDrawerGestureModePanningNavigationBar;
//支持關閉抽屜的手勢類型
drawerCtrl.closeDrawerGestureModeMask = MMCloseDrawerGestureModeAll;
self.window.rootViewController = drawerCtrl;
#endif
ViewController.m文件
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//設置導航條上按鈕的文字或圖標的顏色
// [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];//[UINavigationBar appearance]是在AppDelegate中有效
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
//設置導航條標題顏色,@{}代表Dictionary
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
//設置導航欄顏色透明
//設置導航條顏色透明只需設置NavigationBar的背景圖片爲一張空圖片即可
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsCompact];
//NavigationBar下邊有一個ShadowImage,也可以通過設置空的UIImage設置透明。//這一句本來可以隱藏導航欄底部的分割線,但是不知爲何此時無效,故採取以下另一種方法
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
//隱藏導航欄底部的分割線,navBarHairlineImageView是UIImageView類
navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
//設置導航欄顏色
//self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
//當UIViewController. extendedLayoutIncludesOpaqueBars = NO 時,UINavigationBar添加背景圖片後,UIViewController的視圖的原點座標爲(0, 64), 下移了64px,並且高度縮減了64px。默認NO
// self.extendedLayoutIncludesOpaqueBars = YES;
//導航欄不透明,translucent默認yes,導航條半透明
// self.navigationController.navigationBar.translucent = NO;
//左
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"左側" style:UIBarButtonItemStylePlain target:self action:@selector(didLeftClicked:)];
//右
UIImage *image = [UIImage imageNamed:@"Stat"];
//如果需要顯示圖片的原始模樣,需要設置渲染模式,否則由AppDelegate內的[UINavigationBar appearance]setTintColor所控制
// image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(didRightClicked:)];
//導航條的 度爲44,狀態欄的 度爲20
// UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
// v.backgroundColor = [UIColor purpleColor];
// self.navigationItem.titleView = v; //標題和titleView有衝突,只會有一個有效
// self.title = @"自定義文字刷新";//修改導航條和標題欄標題.在AppDelegate.m內無效
self.navigationItem.title = @"自定義動態圖片刷新";//單單導航條標題
}
#pragma mark 找出導航欄底部橫線
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}
相關屬性
1.導航條UINavigtionBar:
1.1UINavigtionBar的單獨創建
iOS中的導航條可以附着於導航控制器UINavigationController之中使用,也可以在controller中單獨使用
UINavigationBar *bar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];
[self.view addSubview:bar];
1.2導航欄的風格屬性
UINavigationBar默認爲白色的半透明的樣式。導航欄的風格屬性可以通過下面的屬性來設置:
@property(nonatomic,assign) UIBarStyle barStyle;
UIBarStyle是一個枚舉,其中大部分的樣式都已棄用,有效果的只有如下兩個:
typedef NS_ENUM(NSInteger, UIBarStyle) {
UIBarStyleDefault = 0,//默認
UIBarStyleBlack = 1,//黑色
}
1.3導航欄不透明
iOS6後導航欄默認都是半透明的,我們可以通過下面的bool值來設置這個屬性,設置爲NO,則導航欄不透明,默認爲YES的半透明狀態:
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent;
1.4導航條上按鈕的顏色
tintColor這個屬性會影響到導航欄上按鈕的圖案顏色和字體顏色,系統默認是藍色:
//設置導航條上按鈕的文字或圖標的顏色
[[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];
1.5導航條背景顏色
BarTintColor用於設置導航欄的背景色,這個屬性被設置後,半透明的效果將失效
//設置導航條顏色
[[UINavigationBar appearance]setBarTintColor:[UIColor colorWithRed:80.0/255 green:227.0/255 blue:194.0/255 alpha:1.0]];
1.6導航條背景圖片
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (nullable UIImage *)backgroundImageForBarMetrics:(UIBarMetrics)barMetrics;
上面兩個方法用於設置和獲取導航欄的背景圖案,這裏需要注意,默認背景圖案是不做縮放處理的,所以我們使用的圖片尺寸要和導航欄尺寸匹配
//設置導航條上的背景圖片
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"Snip20150916_1.png"] forBarMetrics:UIBarMetricsDefault];
1.7導航條的設備的狀態,豎屏橫屏
UIBarMetrics參數設置設備的狀態,如下:
typedef NS_ENUM(NSInteger, UIBarMetrics) {
UIBarMetricsDefault,//正常豎屏狀態
UIBarMetricsCompact,//橫屏狀態
};
1.8設置導航欄的陰影圖片
@property(nullable, nonatomic,strong) UIImage *shadowImage;
1.9設置導航欄的標題字體屬性
@property(nullable,nonatomic,copy) NSDictionary<NSString *,id> *titleTextAttributes;
標題字體屬性會影響到導航欄的中間標題,如下:
//設置導航條標題顏色,@{}代表Dictionary
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
1.10設置導航欄標題的豎直位置偏移
- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics;
- (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics;
1.10設置導航欄左側pop按鈕的圖案
導航欄左側pop按鈕的圖案默認是一個箭頭,我們可以使用下面的方法修改:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"notebook"] style:UIBarButtonItemStylePlain target:self action:@selector(didRightClicked:)];
1.11設置導航欄顏色透明
//設置導航欄顏色透明
//設置導航條顏色透明只需設置NavigationBar的背景圖片爲一張空圖片即可
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsCompact];
1.11隱藏導航欄底部的分割線
//NavigationBar下邊有一個ShadowImage,也可以通過設置空的UIImage設置透明。//這一句本來可以隱藏導航欄底部的分割線,但是不知爲何此時無效,故採取以下另一種方法
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
//隱藏導航欄底部的分割線,navBarHairlineImageView是UIImageView類
navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
#pragma mark 找出導航欄底部橫線
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}
2.導航欄中item的push與pop操作
UINavigationBar上面不只是簡單的顯示標題,它也將標題進行了堆棧的管理,每一個標題抽象爲的對象在iOS系統中是UINavigationItem對象,我們可以通過push與pop操作管理item組。
//向棧中添加一個item,上一個item會被推向導航欄的左側,變爲pop按鈕,會有一個動畫效果
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
//pop一個item
- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
//當前push到最上層的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
//僅次於最上層的item,一般式被推向導航欄左側的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
//獲取堆棧中所有item的數組
@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
//設置一組item
- (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated;
3.UINavigationBarDelegate
在UINavigationBar中,還有如下一個屬性:
@property(nullable,nonatomic,weak) id<UINavigationBarDelegate> delegate;
通過代理,我們可以監控導航欄的一些push與pop操作:
//item將要push的時候調用,返回NO,則不能push
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item;
//item已經push後調用
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item;
//item將要pop時調用,返回NO,不能pop
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;
//item已經pop後調用
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;