分栏控制器介绍
#import "AppDelegate.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"
#import "ViewController5.h"
#import "ViewController6.h"
@interface AppDelegate ()<UITabBarControllerDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
_window.backgroundColor = [UIColor whiteColor];
[_window makeKeyAndVisible];
//分栏控制器基本的使用方法
UITabBarController *tab = [[UITabBarController alloc] init];
//分栏控制器 相当于一个数组 是有序的
//UITabBarController 的 viewControllers 属性,为一个数组,他所管理的视图控制器都在这个数组中
ViewController1 *vc1 = [[ViewController1 alloc] init];
vc1.title = @"大娃";
ViewController2 *vc2 = [[ViewController2 alloc] init];
vc2.title = @"二娃";
//注意: 一个分栏控制器最多展示5项,超出部分形成一个单元@“More”——-工作当中很少会出现超出5项的情况
ViewController3 *vc3 = [[ViewController3 alloc] init];
vc3.title = @"火娃";
//将vc3作为导航栏的跟视图控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc3];
ViewController4 * vc4 = [[ViewController4 alloc] init];
vc4.title =@"水娃";
ViewController5 *vc5 = [[ViewController5 alloc] init];
vc5.title =@"五娃";
ViewController6 *vc6 = [[ViewController6 alloc] init];
vc6.title =@"隐身娃";
tab.viewControllers = @[vc1,vc2,nav,vc4,vc5,vc6];
//将vc3设置成导航栏根视图
//修改选中项 electedIndex 表示数组的下标
// 方法一
// tab.selectedIndex = 2;
//方法二
tab.selectedViewController = vc4;
[self makeTabBar:tab];
//定制TabBarItem
[self makeTabBarItem:tab];
//取用户选中项
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSNumber *index = [user objectForKey:@"UserSelceted"];
if (index != nil) {
//用户已经操作过,didSelectViewController方法
tab.selectedIndex = [index integerValue];
}
//将UITabBarController 设置成根视图控制器
_window.rootViewController = tab;
return YES;
}
//#pragma <#arguments#>
//能不能选中一个子视器
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
//1.根下标
// NSArray *arr = tabBarController.viewControllers;
//
// NSInteger index = [arr indexOfObject:viewController];
// if (index == 1) {
// return 0;
// }
// return YES;
//2.根据视图控制器属性
if ([viewController.title isEqualToString:@"二娃"]) {
return NO;
}
return YES;
}
//已经选中某个姿势图控制器
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//用户重新启动的时候 会在打开的哪个程序
NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:@(index) forKey:@"UserSelected"];
// if ([viewController.title isEqualToString:@"水娃"]) {
// //
// tabBarController.selectedIndex = 0;
//
// }
}
//
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers {
NSLog(@"用户即将编辑子视图控制器");
}
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed {
NSLog(@"用户即将结束编辑子视图控制器");
}
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed;{
NSLog(@"用户已经结束编辑子视图控制器%d",changed);
//changed 表示用户结束编辑后,是否发生了改变
}
//
- (void)makeTabBarItem:(UITabBarController *)tab{
NSArray *viewControllers = tab.viewControllers;
//
UIViewController *vc1 = viewControllers[0];
//1.系统样式 垃圾 用的少之又少
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:100];
vc1.tabBarItem = item1;
//2.图片 + 标题 + tag
//UITabBarItem优先级大于视图控制器的标题
UIViewController *vc2 = viewControllers[1];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"新二娃" image:[UIImage imageNamed:@"tab_0"] tag:101];
vc2.tabBarItem = item2;
//3.Normal图片 + Selected图片 + 标题
//父类的指针指向子类的对象
UIViewController *vc3 = viewControllers[2];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTitle:@"真火娃" image:[UIImage imageNamed:@"tab_1"] selectedImage:[UIImage imageNamed:@"tab_2"]];
vc3.tabBarItem = item3;
//4.先创建空对像再赋值
UITabBarItem *item4 = [[UITabBarItem alloc] init];
item4.title = @"真水娃";
item4.image = [UIImage imageNamed:@"tab_0"];
item4.selectedImage = [UIImage imageNamed:@"tab_2"];
item4.tag = 104;
UIViewController *vc4 = viewControllers[3];
vc4.tabBarItem = item4;
//设置角标
vc4.tabBarItem.badgeValue = @"99+";
//设置item的偏移量
//<#CGFloat horizontal#>水平
//<#CGFloat vertical#>垂直方向
vc4.tabBarItem.titlePositionAdjustment = UIOffsetMake(15, -10);
}
- (void)makeTabBar:(UITabBarController *)tab{
//一个分栏控制器只有一个tabBar
//默认认为白色半透明
tab.tabBar.barStyle = UIBarStyleBlack;
//设置是否半透明
tab.tabBar.translucent =YES;
//tabBar 高度为49
//bar 的色调
tab.tabBar.barTintColor = [UIColor purpleColor];
//bar中元素的颜色
tab.tabBar.tintColor = [UIColor cyanColor];
//用图片作为背景
[tab.tabBar setBackgroundImage:[UIImage imageNamed:@"header_bg"]];
//tabbar 元素选中的效果图片
tab.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"tab_s"];
// tab.tabBar.itemPositioning = UITabBarSystemItemTopRated;
// tab.tabBar
}
@end
#import "ViewController3.h"
#import "ViewController7.h"
@interface ViewController3 ()
@end
@implementation ViewController3
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
ViewController7 *vc7 = [[ViewController7 alloc] init];
//推入下一级页面是,隐藏Tabbar
//一定要再压栈之前设置
vc7.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc7 animated:YES];
}
注意:创建每个控制器的视图颜色,上面是控制器3的特别添加的一个方法
效果图如下
屏幕快照 2019-04-10 下午9.23.42.png
屏幕快照 2019-04-10 下午9.23.51.png
toolbar详解
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//1 屏幕大小
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//2
self.window.backgroundColor = [UIColor whiteColor];
//3
[_window makeKeyAndVisible];
//4
ViewController *rootVC = [[ViewController alloc] init];
//5
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootVC];
//navigatrion navigationItems
_window.rootViewController = nav;
//更改Toolbar的隐藏状态
[nav setToolbarHidden:NO animated:YES];
//toolbar 属于导航控制器,只有一个,需要统一定制
[self makeToolbarForNav:nav];
return YES;
}
- (void)makeToolbarForNav:(UINavigationController *)nav{
//设置是否透明(默认为半透明)
nav.toolbar.translucent = NO;
//设置toolbar样式(黑白两种)
nav.toolbar.barStyle = UIBarStyleBlack;
//设置toolbar的色调
nav.toolbar.barTintColor = [UIColor orangeColor];
//设置toolbar上元素的色调
nav.toolbar.tintColor = [UIColor whiteColor];
//【注】toolbar是不区分左中右部分的,只有一个部分
//用图片作为toolbar的背景
[nav.toolbar setBackgroundImage:[UIImage imageNamed:@"header_bg"] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
}
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self makeToolbaItems];
}
- (void)makeToolbaItems{
//1.系统样式
//toolbar 一般都是用系统样式 或者图片样式
UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];
//2.文本
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"按钮" style:UIBarButtonItemStylePlain target:nil action:nil];
//3.图片
UIBarButtonItem * item3 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"10_1.jpg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:nil action:nil];
//4.自定义视图
UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithCustomView:sw];
//5.占位符
//(1)固定宽度占位符
//UIBarButtonSystemItemFixedSpace
UIBarButtonItem *spaceitem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spaceitem.width = 40;
//(2)空间均分占位符
//UIBarButtonSystemItemFlexibleSpace
UIBarButtonItem *spaceitem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
self.toolbarItems = @[item1,spaceitem, item2,spaceitem,item4];//用固定占位符来取代item3这个UIBarButtonItem,也就是只有item1,item2,item4显示,item3隐藏掉了
// self.toolbarItems = @[item1,spaceitem1, item2,spaceitem1,item4];//理论同上
// UIBarButtonItem * spaceitem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// self.toolbarItems = @[item1, spaceitem1, item2, spaceitem1, item4];
}
@end
固定宽度占位符使用的效果
固定宽度占位符.png
空间均分占位符使用的效果
空间均分占位符.png
网友评论