- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] init];
self.window.frame = [UIScreen mainScreen].bounds;
[self.window makeKeyAndVisible];
//有一个视图控制器才能有导航控制器
ViewController *vc = [[ViewController alloc] init];
//将navigation嵌套进viewcontroller
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = navigation;
//设置导航栏Bar的颜色
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:34/255.0 green:199/255.0 blue:1.0 alpha:1.0]];
//设置导航栏title字体的颜色,大小
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:25],NSFontAttributeName, nil]];
//设置导航栏返回按钮的颜色
[[UINavigationBar appearance] setTintColor:[UIColor yellowColor]];
//设置运营商、电池、时间字体颜色设置为白色
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack];
return YES;
}
//ViewController
//懒加载
- (UIButton *)pushButton{
if (_pushBtn == nil) {
_pushBtn = [UIButton buttonWithType:UIButtonTypeSystem];
_pushBtn.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width-60)/2, 100, 60, 30);
_pushBtn.backgroundColor = [UIColor redColor];
[_pushBtn setTitle:@"懒加载" forState:UIControlStateNormal];
}
return self.pushBtn;
}
- (void)viewDidLoad {
[super viewDidLoad];
/**
* 导航控制器
UINavigationController 导航控制器的层次结构
继承于UIViewController
导航控制器的作用?
它采用栈的原理来管理视图控制器
(toolbar默认隐藏)
*/
// UINavigationController
self.view.backgroundColor = [UIColor greenColor];
self.navigationController.toolbarHidden = NO;
self.view.window.backgroundColor = [UIColor blueColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(0, 0, 100, 45);
button.backgroundColor = [UIColor colorWithRed:40/255.0 green:176/255.0 blue:249/255.0 alpha:1];
button.layer.borderColor = [[UIColor redColor] CGColor];
button.layer.borderWidth = 2;
button.layer.cornerRadius = 6;
[button setTitle:@"下一页" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.center = self.view.center;
[self.view addSubview:button];
[button addTarget:self action:@selector(pushButtonAction) forControlEvents:UIControlEventTouchDown];
// self.title = @"Root";不推荐使用
//导航栏中间的title
self.navigationItem.title = @"Root";
UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[titleButton setTitle:@"touch me" forState:UIControlStateNormal];
[titleButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[titleButton addTarget:self action:@selector(titleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
// self.navigationItem.titleView = titleButton;
_titleView = [[UIView alloc] initWithFrame:CGRectMake(10, 24, 100, 100)];
_titleView.backgroundColor = [UIColor blueColor];
_titleView.hidden = YES;
// [self.view addSubview:_titleView];
//加载到window上
//拿到window
UIWindow *window = [[UIApplication sharedApplication].windows lastObject];//拿到自己创建window
window.backgroundColor = [UIColor redColor];
[window addSubview:_titleView];
//导航栏左边存在BarButtonItem
UIBarButtonItem *leftBarButtonItem1 = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *leftBarButtonItem2 = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:nil];
// self.navigationItem.leftBarButtonItems = @[];放多个Item
/**设置Item的间距
* UIBarButtonSystemItemFlexibleSpace,系统默认的,不可用设置距离
* UIBarButtonSystemItemFixedSpace,可以设置距离
*
*
*/
UIBarButtonItem *spaceBarbuttonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spaceBarbuttonItem.width = 10;
self.navigationItem.leftBarButtonItems = @[leftBarButtonItem1,spaceBarbuttonItem,leftBarButtonItem2];
[self pushButton];
[self.view addSubview:_pushBtn];
}
- (void)titleButtonAction:(UIButton *)sender {
NSLog(@"点我");
_titleView.hidden = !_titleView.hidden;
}
- (void)pushButtonAction {
NSLog(@"点击到A页面");
AViewController *Avc = [[AViewController alloc] init];
// [self presentViewController:Avc animated:YES completion:^{
// }];模态推送
//当视图控制器被压入导航控制器中,就具备了navigationController
[self.navigationController pushViewController:Avc animated:YES];//导航控制器推送
}
UINavigationBar
最新推荐文章于 2020-10-17 21:56:35 发布