天天看點

navigationController使用, 通過生命周期的隐藏,還有自定義

這是最簡單的通過聲明周期,navigationBar不隐藏  tabBar隐藏 

-(void)viewWillDisappear:(BOOL)animated

{

    self.navigationController.navigationBarHidden = NO;

    self.tabBarController.tabBar.hidden=YES;

}

1 2 3 4 5 6 7 8 9 10 11 12 13

//1.設定self.tabBarController.tabBar.hidden=YES;

self

.tabBarController.tabBar.hidden=

YES

;

//2.如果在push跳轉時需要隐藏tabBar,設定self.hidesBottomBarWhenPushed=YES;這用是通過聲明周期,push故

self

.hidesBottomBarWhenPushed=

YES

;

NextViewController *next=[[NextViewController alloc]init];

[

self

.navigationController pushViewController:next animated:

YES

];

self

.hidesBottomBarWhenPushed=

NO

;

//并在push後設定self.hidesBottomBarWhenPushed=NO;

//這樣back回來的時候,tabBar會恢複正常顯示。

1.隐藏特定UIViewController的導航欄,在該視圖控制器中加入代碼:

- (void) viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:YES];

}

- (void) viewDidDisappear:(BOOL)animated

{

    [super viewDidDisappear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:YES];

} 2.初始化,始終為

[[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] init]] 3.定制自己的UINavigationController,是的push和present的方式變成自己想要的方式,以便統一處理視圖特效

頭檔案:CustomNavigationController.h

@interface CustomNavigationController : UINavigationController

- (UIBarButtonItem *)navBarItemWithTarget:(id)target

                                   action:(SEL)action

                                imageName:(NSString*)imgName;

@end 實作檔案: CustomNavigationController.m

//

//  CustomNavigationController.m

//  PudongTravel

//

//  Created by duwei on 14-4-23.

//  Copyright (c) 2014年 mark. All rights reserved.

//

#import "CustomNavigationController.h"

#import "AppDelegate.h"

@interface CustomNavigationController ()

@end

@implementation CustomNavigationController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

- (void)viewDidLoad

{

    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

- (void)navigationItemInit:(UIViewController*)viewController

{

    UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,40,175,44)];

    titleLabel.backgroundColor = [UIColor clearColor];  //設定Label背景透明

    titleLabel.font            = [UIFont boldSystemFontOfSize:16];  //設定文本字型與大小

    titleLabel.textColor       = [UIColor whiteColor];  //設定文本顔色

    titleLabel.textAlignment   = NSTextAlignmentLeft;

    viewController.navigationItem.titleView = titleLabel;

}

- (UIBarButtonItem *)navBarItemWithTarget:(id)target

                                   action:(SEL)action

                                imageName:(NSString*)imgName

{

    UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];

    leftbutton.frame = CGRectMake(10, 4, 42, 40);

    [leftbutton setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];

    [leftbutton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *letfBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftbutton];

    return letfBarItem;

}

- (void)backBtnPressed:(id)sender

{

    [super popViewControllerAnimated:YES];

}

- (void)dismissLeftBtnPress:(id)sender

{

    [super dismissViewControllerAnimated:YES completion:nil];

}

- (void)rightBtnPressed:(id)sender

{

}

#pragma mark - presentViewController && pushViewController method

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag

                   completion:(void (^)(void))completion

{

    [super presentViewController:viewControllerToPresent animated:flag completion:completion];

    // 添加傳回

    UINavigationController *nav = (UINavigationController*)viewControllerToPresent;

    if ([self.viewControllers count] > 1){

        if (nav.topViewController.navigationItem.leftBarButtonItem == nil){

            nav.topViewController.navigationItem.leftBarButtonItem =

            [self navBarItemWithTarget:self

                                action:@selector(dismissLeftBtnPress:)

                             imageName:@"icon_return.png"];

        }

        if (nav.topViewController.navigationItem.rightBarButtonItem == nil)

        {

            nav.topViewController.navigationItem.rightBarButtonItem = [self navBarItemWithTarget:self action:@selector(rightBtnPressed:) imageName:@"icon_personal.png"];

        }

        [self navigationItemInit:nav.topViewController];

        [self navigationItemInit:nav.topViewController];

    }

}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

{

    [super pushViewController:viewController animated:animated];

    // 添加傳回

    if ([self.viewControllers count] > 1){

        if (viewController.navigationItem.leftBarButtonItem == nil){

            viewController.navigationItem.leftBarButtonItem =

            [self navBarItemWithTarget:self

                                action:@selector(backBtnPressed:)

                             imageName:@"icon_return.png"];

        }

        if (viewController.navigationItem.rightBarButtonItem == nil)

        {

            viewController.navigationItem.rightBarButtonItem =

            [self navBarItemWithTarget:self

                                action:@selector(rightBtnPressed:)

                             imageName:@"icon_personal.png"];

        }

        [self navigationItemInit:viewController];

    }

}

@end 轉載請注明出處:http://www.cppblog.com/TianShiDeBaiGu/