您的位置:首页 > 产品设计 > UI/UE

UI04导航控制器

2016-09-01 19:42 225 查看
1.AppDelegate.m

//创建一个窗口
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
RootViewController *rootVC = [[RootViewController alloc]init];

//初始化导航控制器
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:rootVC];
//走改变状态栏为白色的CustomNavViewController
// CustomNavViewController *nav = [[CustomNavViewController alloc]initWithRootViewController:rootVC];
self.window.rootViewController = nav;

/***导航栏***/
//配置导航栏,半透明效果,关掉半透明效果,才会显示出按钮
//不透明
nav.navigationBar.translucent = NO;
//配置导航栏的背景颜色
nav.navigationBar.barTintColor = [UIColor orangeColor];
//设置按钮颜色
nav.navigationBar.tintColor = [UIColor orangeColor];
//设置背景图片
[nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg_ios7@2x.png"] forBarMetrics:UIBarMetricsDefault];

/**** 工具栏 ****/
//去掉工具栏隐藏
nav.toolbarHidden = NO;
nav.toolbar.barTintColor = [UIColor orangeColor];
[nav.toolbar setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];

[self.window makeKeyAndVisible];
2.RootViewController.h
#import "RootViewController.h"
#import "TwoViewController.h"
@interface RootViewController ()

- (void)initUserInterface;

@end

@implementation RootViewController

- (instancetype)init
{
self = [super init];
if (self) {
// self.title = @"主页";
self.navigationItem.title = @"主页";
}
return self;
}
////隐藏状态栏
//- (BOOL)prefersStatusBarHidden
//{
// return YES;
//}

- (void)viewDidLoad {
[super viewDidLoad];
[self initUserInterface];
}

- (void)initUserInterface{
self.view.backgroundColor = [UIColor whiteColor];
//设置阴影属性
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor grayColor];
shadow.shadowOffset = CGSizeMake(3, 3);
//设置导航栏title的字体和阴影
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor],NSShadowAttributeName:shadow}];

UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
nextButton.frame = CGRectMake(100, 100, 60, 40);
[nextButton setTitle:@"下一页" forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(buttonProcess:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:nextButton];

//加导航栏的按钮
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc]initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(barButtonProcess:)];
//加tag值区分按钮
shareItem.tag = 100;
self.navigationItem.leftBarButtonItem = shareItem;

UIBarButtonItem *shareItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:nil action:@selector(barButtonProcess:)];
//加tag值区分按钮
shareItem1.tag = 101;
self.navigationItem.leftBarButtonItems = @[shareItem,shareItem1];

UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"back_btn@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:nil action:@selector(barButtonProcess:)];
//加tag值区分按钮
rightItem.tag = 102;
self.navigationItem.rightBarButtonItem = rightItem;

//工具栏添加按钮
UIBarButtonItem *toolItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks target:nil action:nil];
UIBarButtonItem *toolItem2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemCamera target:nil action:nil];
4000
UIBarButtonItem *toolItem3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemPlay target:nil action:nil];
//弹簧(设置按钮之间的间隔)
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
//需要固定宽度
// UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
// [spaceItem setWidth:100];

self.toolbarItems = @[toolItem1,spaceItem,toolItem2,spaceItem,toolItem3];

//返回按钮的改变需要到上一个控制器添加(只针对一个)
UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backItem;
}

- (void)barButtonProcess:(UIBarButtonItem *)sender{
NSInteger index = sender.tag - 100;
switch (index) {
case 0:
[self share];
break;

default:
break;
}
}

- (void)share{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"分享" message:@"我的分享" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"新浪微博" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"微信" style:UIAlertActionStyleDestructive handler:nil];
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertVC addAction:action1];
[alertVC addAction:action2];
[alertVC addAction:action3];
[self presentViewController:alertVC animated:YES completion:nil];
}

- (void)buttonProcess:(UIButton *)sender{
TwoViewController *twoVC = [[TwoViewController alloc]init];
[self.navigationController pushViewController:twoVC animated:YES];
}

@end
3.TwoViewController.m
#import "TwoViewController.h"
#import "ThreeViewController.h"
@interface TwoViewController ()
- (void)initUserInterface;
@end

@implementation TwoViewController
- (instancetype)init
{
self = [super init];
if (self) {
// self.title = @"主页";
self.navigationItem.title = @"第二页";
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];
[self initUserInterface];
}

- (void)initUserInterface{
self.view.backgroundColor = [UIColor cyanColor];
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
nextButton.frame = CGRectMake(100, 100, 60, 40);
[nextButton setTitle:@"下一页" forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(buttonProcess:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:nextButton];
}

- (void)buttonProcess:(UIButton *)sender{
ThreeViewController *threeVC = [[ThreeViewController alloc]init];
[self.navigationController pushViewController:threeVC animated:YES];
}
@end
4.ThreeViewController.m
#import "ThreeViewController.h"
#import "TwoViewController.h"
@interface ThreeViewController ()
- (void)initUserInterface;
@end

@implementation ThreeViewController
- (instancetype)init
{
self = [super init];
if (self) {
// self.title = @"主页";
self.navigationItem.title = @"第三页";
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];
[self initUserInterface];
}

- (void)initUserInterface{
self.view.backgroundColor = [UIColor greenColor];
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeSystem];
nextButton.frame = CGRectMake(100, 100, 60, 40);
[nextButton setTitle:@"上一页" forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(buttonProcess:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:nextButton];
}
- (void)buttonProcess:(UIButton *)sender{
//返回上一个控制器

//默认返回上一个控制器
//[self.navigationController popViewControllerAnimated:YES];

//返回到指定的控制器
// self.navigationController.viewControllers[1]获取栈区控制器的下标
// [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];

//直接返回到主页(根控制器)
[self.navigationController popToRootViewControllerAnimated:YES];

}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  导航