UINavigationBar的常用方法,属性

示例代码

#import "AppDelegate.h"
#import "VCRoot.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    //创建一个根视图控制器
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    VCRoot* root = [[VCRoot alloc] init];
    //创建导航控制器
    //导航控制器主要用来管理多个试图控制器的切换
    //层级的方式来管理多个试图控制器
    //创建控制时,一定要有一个根视图控制器
    //参数一:就是作为导航控制器的根视图控制器
    UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:root];

    //window的根视图设置为导航控制器
    self.window.rootViewController = nav;

    [self.window makeKeyAndVisible];

    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
#import "VCRoot.h"

@interface VCRoot ()

@end

@implementation VCRoot

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor yellowColor];

    //设置导航栏的标题文字
    self.title = @"根视图";

    //设置导航元素项的标题
    //如果没有设置navigationItem.title,为nil
    //系统会使用self.titile作为标题
    //就是这个优先度高
    self.navigationItem.title = @"Title";

    //创建一个导航栏左侧的按钮
    //根据title文字来创建按钮
    //P1:按钮上的文字
    //P2:按钮风格
    //P3:事件拥有者
    //P4:按钮事件
    UIBarButtonItem* leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"左侧" style:UIBarButtonItemStyleDone target:self action:@selector(pressLeft)];

    //将导航元素项的左侧按钮赋值
    self.navigationItem.leftBarButtonItem = leftBtn;

    //创建一个导航栏左侧的按钮
    //根据系统风格来创建按钮
    //只需要指定风格样式,系统风格的按钮内容或标题文字不能改变
    //P1:按钮上的文字
    //P2:按钮风格
    //P3:事件拥有者
    //P4:按钮事件
    UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pressRight)];

    self.navigationItem.rightBarButtonItem = rightBtn;

    //标签对象
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 40)];
    label.text = @"test";

    label.textAlignment = NSTextAlignmentCenter;

    //将任何类型的控件添加到导航栏的方法
    UIBarButtonItem* item03 = [[UIBarButtonItem alloc] initWithCustomView:label];

    NSArray* arrayBtn = [NSArray arrayWithObjects:rightBtn, item03, nil];

    //将右侧按钮数组赋值
    self.navigationItem.rightBarButtonItems = arrayBtn;




}

- (void) pressLeft
{
    NSLog(@"左侧按钮被按下!");
}

- (void) pressRight
{
    NSLog(@"右侧按钮被按下!");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
#import "AppDelegate.h"
#import "VCRoot.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen] .bounds];

    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[VCRoot alloc] init]];

    [self.window makeKeyAndVisible];


    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
#import "VCRoot.h"
#import "VCSecond.h"

@interface VCRoot ()

@end

@implementation VCRoot

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //设置导航栏透明度
    //默认透明度为YES,可透明
    self.navigationController.navigationBar.translucent = NO;

    //设置导航栏的风格颜色
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

    self.view.backgroundColor = [UIColor blueColor];

    self.title = @"根视图";

    UIBarButtonItem* next = [[UIBarButtonItem alloc] initWithTitle:@"下一级" style:UIBarButtonItemStylePlain target:self action:@selector(pressNext)];

    self.navigationItem.rightBarButtonItem = next;
}

- (void) pressNext
{
    //创建一个新的视图控制器
    VCSecond* vcSecond = [[VCSecond alloc] init];

    //使用当前视图控制器的导航控制器对象
    [self.navigationController pushViewController:vcSecond animated:YES];



}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
#import "VCSecond.h"
#import "VCThird.h"
@interface VCSecond ()

@end

@implementation VCSecond

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor yellowColor];

    self.title = @"视图二";

    UIBarButtonItem* btnNext = [[UIBarButtonItem alloc] initWithTitle:@"第三级" style:UIBarButtonItemStylePlain target:self action:@selector(pressNext)];

    self.navigationItem.rightBarButtonItem = btnNext;
}

- (void) pressNext
{
    //创建一个新的视图控制器
    VCThird* vcThird = [[VCThird alloc] init];

    //推入第三个视图控制器
    [self.navigationController pushViewController:vcThird animated:YES];



}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
#import "VCThird.h"

@interface VCThird ()

@end

@implementation VCThird

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor greenColor];

    self.title = @"第三级";

    UIBarButtonItem* btnLeft = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(pressBack)];


    //当自己设定左侧按钮时
    //返回键会被左侧按钮替换

    self.navigationItem.leftBarButtonItem = btnLeft;
}

- (void) pressBack
{
    NSLog(@"返回上一级");
    //将当前的试图控制器弹出,返回到上一级界面
    [self.navigationController popViewControllerAnimated:YES];

    //[self.navigationController popToRootViewControllerAnimated:YES];
    //直接返回根视图
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

心得体会

  1. 首先,我们要有导航控制器,根视图控制器,将window的根视图设置为导航控制器
  2. 使用BarButton大法,把任何控件添加到导航栏
  3. 前进 push 后退 pop
  4. 记住第一步设置根视图
  5. 到新的界面需要一个新的界面控制器

猜你喜欢

转载自blog.csdn.net/KevinAshen/article/details/81176794