常用控制器(navigation、tabBar)
AppDelegate
#import "AppDelegate.h"
#import "NavViewController.h"
#import "TabBarViewController.h"
#import "SecondTabBarViewController.h"
#import "ThirdTabBarViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// self.window.backgroundColor = [UIColor whiteColor];
// //导航控制器
// NavViewController * navVC = [[NavViewController alloc] init];
// UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:navVC];
// //设置导航条背景色,此处设置则导航控制器的所有视图都是黑色
// nav.navigationBar.barTintColor = [UIColor blackColor];
// self.window.rootViewController = nav;
//
// //简单方法创建导航控制器
// //self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[NavViewController alloc] init]];
// [self.window makeKeyAndVisible];
// return YES;
//tabBar
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//新建并初始化一个tabBar控制器
UITabBarController * tabBar = [[UITabBarController alloc] init];
//设置为根控制器
self.window.rootViewController = tabBar;
//子控制器
NavViewController * navVC = [[NavViewController alloc] init];
//导航控制器
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:navVC];
//导航栏颜色
nav.navigationBar.barTintColor = [UIColor redColor];
nav.view.backgroundColor = [UIColor whiteColor];
nav.tabBarItem.title = @"NavDemo";
nav.tabBarItem.image = [UIImage imageNamed:@"个人中心iocn"];
//设置出现的红数字
nav.tabBarItem.badgeValue = @"1";
TabBarViewController * tabBarVC = [[TabBarViewController alloc] init];
UINavigationController * navTabBarVC = [[UINavigationController alloc] initWithRootViewController:tabBarVC];
navTabBarVC.view.backgroundColor = [UIColor grayColor];
navTabBarVC.tabBarItem.title = @"工作";
//自定义标题和图片
[navTabBarVC.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[navTabBarVC.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
navTabBarVC.tabBarItem.image = [[UIImage imageNamed:@"工作icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navTabBarVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"工作icon2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navTabBarVC.tabBarItem.badgeValue = @"99+";
//[tabBar addChildViewController:navTabBarVC];
SecondTabBarViewController * tabBarVC2 = [[SecondTabBarViewController alloc] init];
UINavigationController * navTabBarVC2 = [[UINavigationController alloc] initWithRootViewController:tabBarVC2];
navTabBarVC2.view.backgroundColor = [UIColor blackColor];
navTabBarVC2.tabBarItem.title = @"沟通";
[navTabBarVC2.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[navTabBarVC2.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
navTabBarVC2.tabBarItem.image = [[UIImage imageNamed:@"沟通icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navTabBarVC2.tabBarItem.selectedImage = [[UIImage imageNamed:@"沟通icon2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
ThirdTabBarViewController * tabBarVC3 = [[ThirdTabBarViewController alloc] init];
UINavigationController * navTabBarVC3 = [[UINavigationController alloc] initWithRootViewController:tabBarVC3];
navTabBarVC3.view.backgroundColor = [UIColor grayColor];
navTabBarVC3.tabBarItem.title = @"通讯录";
[navTabBarVC3.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[navTabBarVC3.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
navTabBarVC3.tabBarItem.image = [[UIImage imageNamed:@"通讯录icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navTabBarVC3.tabBarItem.selectedImage = [[UIImage imageNamed:@"通讯录icon2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//TabBar栏颜色
//tabBar.tabBar.BarTintColor = [UIColor blackColor];
//不能与addChildViewController混用
tabBar.viewControllers = @[nav, navTabBarVC, navTabBarVC2, navTabBarVC3];
[self.window makeKeyAndVisible];
return YES;
}
NavViewController
#import "NavViewController.h"
#import "SecondViewController.h"
@interface NavViewController ()
@end
@implementation NavViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.tabBarItem.badgeValue = @"0";
//self.tabBarController.tabBar.barTintColor = [UIColor greenColor];
self.view.backgroundColor = [UIColor whiteColor];
//设置标题
self.navigationItem.title = @"学习UINavigationController";
//设置标题颜色(字体等)
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
//自定义右按钮项
UIButton * personBtn = [UIButton buttonWithType:UIButtonTypeCustom];
personBtn.frame = CGRectMake(0, 0, 29, 29);
[personBtn setImage:[UIImage imageNamed:@"个人中心iocn"] forState:UIControlStateNormal];
[personBtn addTarget:self action:@selector(person) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithCustomView:personBtn];
[self.navigationItem setRightBarButtonItem:rightItem];
//back通过这种方式设置,设置的是下一级的back,系统自带的是left的返回
UIBarButtonItem * backItem2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backItem2;
}
- (void)back
{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)person
{
[self.navigationController pushViewController:[[SecondViewController alloc] init] animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Second
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
//自定义title视图
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CenterNews"]];
//跳转手势
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(next)];
[self.view addGestureRecognizer:tap];
}
- (void)next
{
[self.navigationController pushViewController:[[ThirdViewController alloc] init] animated:YES];
}
Third
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
//导航栏隐藏
//self.navigationController.navigationBar.hidden = YES;
self.navigationItem.title = @"TabBarDemo";
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil];
//该方法不能实现完全自定义,颜色是蓝色
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
//自定义的左按钮项,返回
// UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// backBtn.frame = CGRectMake(0, 0, 29, 29);
// [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
// [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
// UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
// [self.navigationItem setLeftBarButtonItem:leftItem];
}
- (void)back
{
//返回根视图控制器
//[self.navigationController popToRootViewControllerAnimated:YES];
//返回相对于当前视图控制器前i层的控制器
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] - 2)] animated:YES];
}