iOS开发之UI(七)

1.UINavigationController

UINavigationController(导航视图控制器):管理控制器的控制器,主要管理有层次递进关系的控制器,以栈的方式管理所控制的视图控制器,至少要由一个被管理的试图控制器,这个视图控制器叫导航视图控制器的根视图控制器

创建方法,在Appdelegate.m中创建

ViewController *VC = [[ViewController alloc] init];
UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:VC];
self.window.rootViewController = naVC;

2.UINavigationBar

UINavigationBar(导航栏):iOS7之后默认是半透明,iOS7之前默认是不透明的。竖屏下默认高度44,横屏下默认高度32。iOS7之后,navigationBar的背景会延生到statusBar上,所以显示效果为64。每个视图控制器都有一个navigationItem属性,在其中设置左按钮、右按钮、标题等,会随着控制器的显示,也显示到navigationBar上

导航栏属性

self.navigationItem.title = @"标题";// 创建单个titile
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];// 创建单个leftBarButtonItem
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];// 创建单个rightBarButtonItem
/*
// 创建多个titles
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"我", @"你"]];
segmentedControl.frame = CGRectMake(0, 0, 100, 30);
segmentedControl.selectedSegmentIndex = 0;
self.navigationItem.titleView = segmentedControl;

// 创建多个BarButtonItems
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(add)];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)];
self.navigationItem.leftBarButtonItems = @[item1, item2];
self.navigationItem.rightBarButtonItems = @[item1, item2];

// UINavigationBar属性
self.navigationController.navigationBarHidden = NO;// 显隐性
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;// 样式
self.navigationController.navigationBar.backgroundColor = [UIColor redColor];;// 背景颜色
//self.navigationController.navigationBar.barTintColor = [UIColor yellowColor];// 前景颜色
self.navigationController.navigationBar.tintColor = [UIColor blackColor];// 元素颜色
 */

/**
 *  半透明效果开启时,坐标原点为(0, 0)
    半透明效果关闭时,坐标原点为(64, 0)
 */
self.navigationController.navigationBar.translucent = YES;// 半透明效果,iOS7以后默认为YES

3.页面跳转

导航视图控制器push

- (void)add {
    BlueViewController *blueVC = [[BlueViewController alloc] init];
    [self.navigationController pushViewController:blueVC animated:YES];// 使用导航视图控制器推出blueVC
}

模态推出

- (void)save {
    BlueViewController *blueVC = [[BlueViewController alloc] init];
    [self presentViewController:blueVC animated:YES completion:nil];// 模态推出blueVC
}

返回方法

- (void)back {
    //[self.navigationController popToRootViewControllerAnimated:YES];// 返回根视图控制器
    //[self.navigationController popViewControllerAnimated:YES];// 返回上一个视图控制器
    [self dismissViewControllerAnimated:YES completion:nil];// 模态返回上一页

}

你可能感兴趣的:(iOS开发之UI(七))