效果图
项目分析
三级视图控制器
- 标签控制器 -> 导航控制器 -> 视图控制器,这就叫三级视图控制器
- 一个导航控制器对应于一个视图控制器,
- 标签控制器是管理固定的几个视图控制器,子控制器是并列的。
- UITabBarController用数组管理视图控制器,而导航控制器所管理的视图控制器之间的关系是上下级关系
设置window
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController *rootVC = [[RootViewController alloc] init];
//权限最高的给根视图控制器
self.window.rootViewController = rootVC;
// Override point for customization after application launch.
return YES;
}
数组赋值示例
NSArray *vcArray = @[profielVC,messageVC,colaVC,userVC,moreVC];
NSMutableArray *tabArray = [NSMutableArray arrayWithCapacity:vcArray.count];
背景颜色赋值为图片
_tabBarView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mask_navbar"]];
UIButton设tag值
//区分是点击了哪个button
//iOS中100以前的tag值有特殊用法,所以最好在100以后
btn.tag = 100 + i;
UITabBarController的属性
//选择的是哪个tab
self.selectedIndex = button.tag - 100;
//隐藏系统默认的样式
self.tabBar.hidden = YES;
//用数组管理视图控制器
self.viewControllers = tabArray;
Tab子视图调用根视图方法(UITabBarController)
RootViewController *rootVC = (RootViewController *)self.tabBarController;
[rootVC showTabBar:NO];
动画效果在于CGPoint or frame的改变
[UIView animateWithDuration:0.2 animations:^{
_selectView.center = button.center;
} completion:nil];
//重新赋值frame,让tabBar消失
[UIView animateWithDuration:0.2 animations:^{
self.tabBarView.frame = frame;
} completion:nil];
UIButton的设置
//需要custom类型才能设置图片
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//[pushButton setImage:(UIImage *) forState:(UIControlState)]
//标题和图片不能同时设置
[pushButton setTitle:@"Push" forState:UIControlStateNormal];
视图顶部导航栏左右按钮
//自定义导航栏按钮
- (void)initNavButton{
UIButton *writeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//导航按钮的xy值没有用
writeBtn.frame = CGRectMake(0, 0, writeButtonWidth, writeButtonHeight);
[writeBtn setBackgroundImage:[UIImage imageNamed:@"write"] forState:UIControlStateNormal];
[writeBtn addTarget:self action:@selector(presentAction) forControlEvents:UIControlEventTouchUpInside];
//添加自定义按钮
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:writeBtn];
self.navigationItem.rightBarButtonItem = item;
}
视图跳转 - 模态视图
呈现使用present
[self presentViewController:modalVC animated:YES completion:nil];
退出使用dissmiss
- (void)dismissAction{
[self dismissViewControllerAnimated:YES completion:nil];
}
视图跳转 - push视图
呈现用导航视图push/show
- (void)pushAction{
PushViewController *pushVC = [[PushViewController alloc] init];
[self.navigationController pushViewController:pushVC animated:YES];
//[self.navigationController showViewController:pushVC sender:nil];
RootViewController *rootVC = (RootViewController *)self.tabBarController;
[rootVC showTabBar:NO];
}
退出用pop 的三种方式
- (void)popAction{
// 回到上级视图
[self.navigationController popViewControllerAnimated:YES];
// 跳转到某视图
//[self.navigationController popToViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#>]
// 跳转到根视图
//[self.navigationController popToRootViewControllerAnimated:YES];
}