欢迎访问宙启技术站
智能推送

iOS中导航栏的基本使用汇总

发布时间:2023-05-15 02:10:49

在iOS中,导航栏是一个非常常用的组件,可以用来实现页面跳转,展示页面标题、返回按钮等内容。下面汇总了iOS中导航栏的基本使用方法:

1. 创建导航控制器

在iOS中,我们通常使用导航控制器来管理多个视图控制器,因此,首先需要创建一个导航控制器。我们可以在AppDelegate中创建导航控制器对象,然后将根视图控制器设置为导航控制器。

// 创建导航控制器对象
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:rootViewController];

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

2. 设置导航栏标题

在导航栏中设置标题非常简单,只需要设置当前视图控制器的title属性即可。

self.title = @"导航栏标题";

3. 设置导航栏按钮

导航栏中最常见的按钮是返回按钮和右侧按钮。下面分别介绍如何设置这两种按钮。

(1)返回按钮

在iOS中,返回按钮会自动在导航栏的左侧显示,并且会显示上一个视图控制器的标题或者一个自定义的返回按钮图标。如果需要自定义返回按钮的文字或者图片,可以重写返回按钮的方法。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 设置自定义返回按钮
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backButton;
}

(2)右侧按钮

右侧按钮可以用来触发一些操作,比如搜索、分享、编辑等。可以通过设置当前视图控制器的rightBarButtonItem属性来设置右侧按钮,也可以设置多个按钮。

// 设置一个右侧按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;

// 设置多个右侧按钮
UIBarButtonItem *button1 = [[UIBarButtonItem alloc]initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(shareButtonClicked)];
UIBarButtonItem *button2 = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(editButtonClicked)];
NSArray *buttonArray = [[NSArray alloc]initWithObjects:button1,button2,nil];
self.navigationItem.rightBarButtonItems = buttonArray;

4. 隐藏导航栏

有时候,我们需要隐藏导航栏,比如全屏展示图片的时候或者启动欢迎页面的时候。可以通过设置导航栏的隐藏属性来实现。

// 隐藏导航栏
self.navigationController.navigationBarHidden = YES;

// 显示导航栏
self.navigationController.navigationBarHidden = NO;

5. 设置导航栏颜色和背景图片

可以通过设置导航栏的tintColor属性和barTintColor属性来设置导航栏的颜色,也可以设置导航栏的背景图片。需要注意的是,设置导航栏的背景图片后,tintColor和barTintColor属性会被忽略。

// 设置导航栏的颜色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];

// 设置导航栏的背景图片
UIImage *backgroundImage = [UIImage imageNamed:@"nav_bg_image"];
[self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];

6. 自定义导航栏

有时候,我们需要完全自定义导航栏,比如在一些特殊场景下需要用到自定义导航栏。在iOS中,我们可以通过设置导航栏的背景视图来实现自定义导航栏。

// 创建一个自定义导航栏视图
UIView *customNavigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
customNavigationView.backgroundColor = [UIColor blueColor];

// 设置导航栏背景视图
[self.navigationController.navigationBar addSubview:customNavigationView];
[self.navigationController.navigationBar sendSubviewToBack:customNavigationView];

// 隐藏系统的导航栏
[self.navigationController setNavigationBarHidden:YES animated:YES];

7. 设置状态栏样式

在iOS中,状态栏的样式可以设置为黑色或白色,并且可以设置状态栏的背景颜色或者背景图片。通常情况下,状态栏的样式会与导航栏的样式一致。

// 设置状态栏样式为白色
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

// 设置状态栏的背景颜色
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
statusBarView.backgroundColor = [UIColor blueColor];
[self.view addSubview:statusBarView];

到此,iOS中导航栏的基本使用已经介绍完毕,希望本文能对大家有所帮助。