UINavigationController

注意点:在iOS7之后,导航控制器下所有UIScrollView顶部都会添加额外的滚动区域(64)self.tableView.contentInset.top(主流框架搭建)

UINavigationController的简单使用

UINavigationController的使用步骤

  1. 初始化UINavigationController
  2. 设置UIWindow的rootViewController为UINavigationController
  3. 根据具体情况,通过push方法添加对应个数的子控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    // 创建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    // 创建导航控制器的跟控制器,也属于导航控制器的子控制器
    UIViewController *vc = [[OneViewController alloc] init];
    vc.view.backgroundColor = [UIColor redColor];


    // 导航控制器也需要一个根控制器
    // 默认导航控制器把根控制器的view添加到导航控制器的view上
    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];

    // 设置窗口的跟控制器
    self.window.rootViewController = navVc;

    [self.window makeKeyAndVisible];


    return YES;
}

UINavigationController的子控制器

UINavigationController以栈的形式保存子控制器
@property(nonatomic,copy) NSArray *viewControllers;
@property(nonatomic,readonly) NSArray *childViewControllers;
使用push方法能将某个控制器压入栈
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
使用pop方法可以移除控制器

将栈顶的控制器移除

- (UIViewController *)popViewControllerAnimated:(BOOL)animated;

回到指定的子控制器

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;

//示例,回到指定控制器
- (IBAction)twoVC:(id)sender {
    for (UIViewController *vc in self.navigationController.childViewControllers) {
        if ([vc isKindOfClass:[TwoViewController class]]) {
            [self.navigationController popToViewController:vc animated:YES];
        }
    }
}

回到根控制器(栈底控制器)

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

如何修改导航栏的内容

导航栏的内容由栈顶控制器的navigationItem属性决定

UINavigationItem有以下属性影响着导航栏的内容

左上角的返回按钮

@property(nonatomic,retain) UIBarButtonItem *backBarButtonItem;

中间的标题视图

@property(nonatomic,retain) UIView          *titleView;

//示例
self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];

中间的标题文字

@property(nonatomic,copy)   NSString        *title;

//示例
self.title = @"第一个导航控制器";
//or
self.navigationItem.title = @"第一个控制器";

左上角的视图

@property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;

//示例
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:0 target:nil action:nil];

右上角的视图

@property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;

//示例
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"navigationbar_friendsearch"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"navigationbar_friendsearch_highlighted"] forState:UIControlStateHighlighted];
    // 导航条上面的内容位置不能由开发者决定,开发者只能控制尺寸
//    btn.frame = CGRectMake(2000, 3000, 30, 30);

    // 控件的尺寸由图片决定
    // 仅仅是设置尺寸
    [btn sizeToFit];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];

results matching ""

    No results matching ""