UI学习 第八章 导航控制器
方法
-(
instancetype
)init
//
初始化
-(void)loadView//加载视图
- (void)viewDidLoad
//
视图加载完成
-(
void
)viewWillAppear:(
BOOL
)animated
//
视图即将出现
-(void)viewDidAppear:(BOOL)animated //视图出现
-(
void
)viewWillDisappear:(
BOOL
)animated
//
即将消失
-(void)viewDidDisappear:(BOOL)animated 消失
-(void)dealloc 控制器被销毁
重要方法
1.视图跳转(从一个视图控制器跳到另外一个视图控制器)
SecondViewController
*secondVc = [[
SecondViewController
alloc
]
init
];
[self presentViewController:secondVc animated:YES completion:nil];
2.让一个视图控制器消失(不是销毁)
[
self
dismissViewControllerAnimated
:
YES
completion
:
nil
];
3.正向传值
第一步:在要接收值得那个视图控制器里定义一个接受值类型的属性;
第二步:在原来的视图控制器对属性进行赋值;
第三部:在后来的视图控制器里可以直接用这个属性的值;
Appdelegete.m
ViewController
*vc = [[
ViewController
alloc
]
init
];
//
导航控制器的根视图
UINavigationController
*nav = [[
UINavigationController
alloc
]
initWithRootViewController
:vc];
//
初始化导航控制器
self.window.rootViewController = nav;//设置window的根视图控制器为导航控制器
1.导航条默认半透明,这时会遮挡
self.view
的
64
个像素的高度
(tableView
除外
)
,为了避免这种情况,我们可以将导航条的半透明效果关掉
self
.
navigationController
.
navigationBar
.
translucent
=
NO
;
2.设置导航条标题
self
.
title
=
@"
导航条
"
;
3.修改标题颜色
self
.
navigationController
.
navigationBar
.
titleTextAttributes
= [
NSDictionary
dictionaryWithObjectsAndKeys
:[
UIColor
whiteColor
],
NSForegroundColorAttributeName
,
nil
];
4.导航条背景色修改
self
.
navigationController
.
navigationBar
.
barTintColor
= [
UIColor
purpleColor
];
5.
设置背景图,注意背景图片尺寸的合理性,避免平铺带来的视觉
bug(有小圆角)
[
self
.
navigationController
.
navigationBar
setBackgroundImage
:[
UIImage
imageNamed
:
@"1"
]
forBarMetrics
:
UIBarMetricsDefault
];
方法
1.跳转视图
-(
void
)gotoNext{
SecondViewController
*secondVc = [[
SecondViewController
alloc
]
init
];
//nav 跳转方式
//nav 跳转方式
[self.navigationController pushViewController:secondVc animated:YES];
}
2.跳回来,注意是返回上级
-(
void
)back{
[ self . navigationController popViewControllerAnimated : YES ]; // 返回上级
[ self . navigationController popViewControllerAnimated : YES ]; // 返回上级
}
3.导航器隐藏
[
self
performSelector
:
@selector
(hidden)
withObject
:
nil
afterDelay
:
1
];
-(
void
)hidden{
[ self . navigationController setNavigationBarHidden : YES animated : YES ];
[ self . navigationController setNavigationBarHidden : YES animated : YES ];
}
4.自定义导航条按钮
UIButton
*backBtn = [
UIButton
buttonWithType
:
UIButtonTypeCustom
];
[backBtn setImage :[ UIImage imageNamed : @"back" ] forState : UIControlStateNormal ];
[backBtn setImage :[ UIImage imageNamed : @"back" ] forState : UIControlStateNormal ];
backBtn.frame = CGRectMake(0, 0, 30, 40);
[backBtn
addTarget
:
self
action
:
@selector
(back)
forControlEvents
:
UIControlEventTouchUpInside
];
UIBarButtonItem *item = [[ UIBarButtonItem alloc ] initWithCustomView :backBtn];
UIBarButtonItem *item = [[ UIBarButtonItem alloc ] initWithCustomView :backBtn];
self.navigationItem.leftBarButtonItem = item;
5.自定义导航条头视图
UISegmentedControl
*seg = [[
UISegmentedControl
alloc
]
initWithItems
:
@[
@"1"
,
@"2"
]
];
seg.frame = CGRectMake(0, 0, 80, 40);
self.
navigationItem.
titleView = seg;