iOS App开发中导航栏的创建及基本属性设置教程

时间:2021-05-20

文件目录如下:基本导航顺序: root -> First -> Second -> Third。其中,FirstViewController作为 navigation堆栈的rootview

1、创建navigation

如果是想直接把navigation导航作为项目一开始的跟视图,把RootViewController.h文件里的nav属性放到AppDelegate.h里即可,再把RootViewController.m文件里的action的代码复制到 AppDelegate.m里的didFinishLaunchingWithOptions 方法里,最后把 self.window.rootViewController 设置 UINavigationController类型的属性nav即可

在RootViewController.h文件
复制代码 代码如下:
#import <UIKit/UIKit.h>
@class FirstViewController;

@interface RootViewController : UIViewController

@property (strong, nonatomic) UINavigationController *nav;

- (IBAction)btnClick:(UIButton *)sender;

@end

在RootViewController.m 文件里的随意一个自定义action里:
复制代码 代码如下:
- (IBAction)btnClick:(UIButton *)sender {

//创建一个viewcontroller
FirstViewController *fristview =[[[FirstViewController alloc] init] autorelease];


//初始化UINavigationController(方式一)
self.nav = [[[UINavigationController alloc] initWithRootViewController:fristview] autorelease];


//初始化UINavigationController(方式二)
// self.nav = [[[UINavigationController alloc] init] autorelease];
// [self.nav pushViewController:fristview animated:YES];


//初始化UINavigationController(方式三,失败,xib文件加载失败,原因暂时不明)
// self.nav = [[[UINavigationController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];


//跳转到FirstView页面
[self presentViewController:self.nav animated:YES completion:nil];


//这种写法一般用于往view里添加一些小控件,如button label textField之类的,不适宜用于页面跳转
// [self.view addSubview:self.nav.view];


}

2.navigation的常用属性设置例子
我们的navigation就加载上去了以后,下面我们来设置navigation的属性:
复制代码 代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明
self.title = @"navigationcontroller";//设置navigationbar上显示的标题
[self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]];//设置navigationbar的颜色
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:Nil];//设置navigationbar左边按钮
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:Nil];//设置navigationbar右边按钮
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];//设置navigationbar上左右按钮字体颜色
}

效果图如下:

这里还有一个属性常用,就是:

复制代码 代码如下:

NSArray *arr = [NSArray arrayWithObjects:@"1",@"2", nil nil];
UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:arr];
self.navigationItem.titleView = segment;//设置navigation上的titleview

效果如下:

对,我们看到中间的字变成了两个可选的按钮,这就是navigation的另一个属性:navigationitem.titleview。

下面我们再建立一个视图,看一下两个视图之前是怎样通信的。

在第二个视图中,我添加了一个button来显示,并加了一个成员变量来接收从第一个视图中穿过来的值:

复制代码 代码如下:
@interface SecondViewController : UIViewController
@property (copy,nonatomic) NSString *str;
@end

复制代码 代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"second";
UIButton *aBUTTON = [[UIButton alloc]initWithFrame:CGRectMake(30, 30, 50, 30)];
[aBUTTON setTitle:_str forState:UIControlStateNormal];
[aBUTTON addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aBUTTON];
}

然后我将第一个视图的右边按钮添加一个事件,点击按钮,就会推出第二个视图,并显示我们传过来的值:

复制代码 代码如下:
- (void)clicked{
SecondViewController *second = [[SecondViewController alloc]init];
[self.navigationController pushViewController:second animated:YES];
second.str = @"hello!!";
[second release];
}

下面,我们来运行一下:

点进按钮以后,我们的第二个视图推出,button显示了传过来的值。

然后我们点击回button,还有navigation另外一个方法:
复制代码 代码如下:
- (void)clicked{
[self.navigationController popViewControllerAnimated:YES];
}

这样就可以回到第一个视图。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章