Appdelegate.m
#import "AppDelegate.h"
#import "FJUINavigationController.h"
#import "FirstViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
_window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[_window setBackgroundColor:[UIColor whiteColor]];
[_window makeKeyAndVisible];
//==================导航控制器的定制====================
//1.导航条,属于导航控制器的
//2.导航选项:navigationItem:属于视图控制器上的
//3.工具条:toolBar:属于导航控制器;
//4.toolBarItem:显示在工具条上的内容;属于视图控制器;
#pragma mark - 定制导航条
//==================定制导航条=========================
//1.创建一个视图控制器
FirstViewController *firstViewController =[[FirstViewController alloc]init];
//2.设置为导航控制器的根视图控制器
FJUINavigationController *FJNavigationController = [[FJUINavigationController alloc]initWithRootViewController:firstViewController];
//3.设置导航控制器作为window的根视图控制器;
_window.rootViewController = FJNavigationController;
return YES;
}
@end
FJUInavigationController.m
#import "FJUINavigationController.h"
@interface FJUINavigationController ()
@end
@implementation FJUINavigationController
#pragma mark -生命周期
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self navigationBarSetting];
}
#pragma mark - 定制导航条
//注意:导航条设置的所有东西,这个导航控制器的所有视图控制器的
//导航条就都变成一样的.
- (void)navigationBarSetting{
//想要定制导航条必须先拿到导航条对象(导航条属于导航控制器)
//1.设置是否有透明度(默认导航条是有透明度的)
[self.navigationBar setTranslucent:YES];
//2.设置导航条样式;UIBarStyleBlack那么状态栏的字是白色
[self.navigationBar setBarStyle:UIBarStyleBlack];
//3.设置导航条的颜色,这个改变的是导航条的背景色
[self.navigationBar setBarTintColor:[UIColor lightGrayColor]];
//4.设置导航条填充颜色(可以改变返回键的颜色);
[self.navigationBar setTintColor:[UIColor orangeColor]];
//5.改变标题的字体和颜色
[self.navigationBar setTitleTextAttributes:@{
NSFontAttributeName:[UIFont systemFontOfSize:30],
NSForegroundColorAttributeName:[UIColor magentaColor]}];
//6.设置导航条的背景图片
//iphone上所有的状态栏的高度:20
//导航条的高度:20 + 44 = 64;
//如果图片高度>=64像素,那么设置的图片的作用范围是状态栏加上导航条(20+44);
//如果图片高度小于64,那么图片范围只是导航条(44);
[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"header_bg64.png"] forBarMetrics:UIBarMetricsDefault];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
#pragma mark -添加button和事件
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];
button.backgroundColor = [UIColor orangeColor];
[button addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//设置标题
self.title = @"首页";
//================定制navigationItem==========
[self navigationItemSetting];
}
#pragma mark - 定制navigationItem
- (void)navigationItemSetting{
//navigationItem就是一个视图控制器所在显示的
//导航条上的内容,navigationItem属于视图控制器
//1.设置titleView
//(1)如果中间只是显示文字,可以通过直接设置视图控制器的title属性来设置
[self.navigationItem setTitle:@"我的首页" ];
//(2)如果中间显示其他的视图,如果设置了视图会覆盖原来的文字部分
UISwitch *sw = [[UISwitch alloc]initWithFrame:
CGRectMake(0, 0, 0, 0)];
sw.on = YES;
UISearchBar *search = [[UISearchBar alloc]initWithFrame:
CGRectMake(0, 0, 100, 40)];
UIView *view = [[UIView alloc]initWithFrame:
CGRectMake(0, 0, 100, 40)];
[view addSubview:search];
[self.navigationItem setTitleView:search];
//2.设置右边的navigationItem
//创建一个BarButtonItem
//(1).创建一个系统的item
// UIBarButtonSystemItemDone,
// UIBarButtonSystemItemCancel,
// UIBarButtonSystemItemEdit,
// UIBarButtonSystemItemSave,
UIBarButtonItem * item1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self action:@selector(onclicked2)];
//(2)通过自定制view的方式创建item;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:
CGRectMake(0, 0, 30, 30)];
imageView.image = [UIImage imageNamed:@"itemImage2"];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:imageView];
//(3)通过文字创建可以点击的item
UIBarButtonItem *item3 = [[UIBarButtonItem alloc]
initWithTitle:@"鹿校草" style:UIBarButtonItemStylePlain
target:self action:@selector(onclicked2)];
//(4)通过图片创建可以点击的按钮
//设置图片的时候需要设置图片的渲染模式
//为:UIImageRenderingModeAlwaysOriginal否则图片不会正确显示;
UIBarButtonItem *item4 = [[UIBarButtonItem alloc]initWithImage:
[[UIImage imageNamed:@"itemImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain target:self
action:@selector(onclicked2)];
//将item1设置为rightItem
//在这个方法的内部创建了一个对应的按钮;
[self.navigationItem setRightBarButtonItem:item4];
//通过数组来设置右边items的时候,会覆盖单独设置的右边的item;
[self.navigationItem setRightBarButtonItems:@[item4,item3]];
//3.设置左边的item
[self.navigationItem setLeftBarButtonItem:item4];
[self.navigationItem setLeftBarButtonItems:@[item2,item1]];
[self.navigationItem setRightBarButtonItem:item1];
[self.navigationItem setRightBarButtonItems:@[item1,item3]];
[self.navigationItem setLeftBarButtonItems:@[item4,item2,]];
}
#pragma mark - item里的button被点击
- (void) onclicked2{
NSLog(@"item里的button被点击");
}
#pragma mark -按钮点击
- (void)onclicked:(UIButton *)btn{
SecondViewController *se = [[SecondViewController alloc]init];
[self.navigationController pushViewController:se animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end