自定製UINavigationBar和navigationBarItem

Appdelegate.m


#import "AppDelegate.h"數組

#import "FJUINavigationController.h"app

#import "FirstViewController.h"iphone

@interface AppDelegate ()ide


@end 工具


@implementation AppDelegate字體



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {spa

    // Override point for customization after application launch..net

     _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];3d

    

    [_window setBackgroundColor:[UIColor whiteColor]];code

    [_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

相關文章
相關標籤/搜索