导航控制器的使用03

来源:互联网 发布:java转smali工具 编辑:程序博客网 时间:2024/05/20 05:05
    • 每个UIViewController都有一个navigationController可以拿到它的导航控制器
    • 还有个navigationItem拿到它自己的导航栏
      • 通过导航栏可以设置它左、右两侧的按钮,也可以设置多个按钮
      • 设置顶部的标题
      • 设置顶部的标题为一个控件
    • 设置导航栏标题、及左右两侧按钮
    • #pragma mark - 设置标题// 1.设置标题self.navigationItem.title = @"红色";#pragma mark - 设置左侧按钮UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(item1Click)];UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];// 2.1.设置单个itemself.navigationItem.leftBarButtonItem = item1;#pragma mark - 设置右侧按钮self.navigationItem.rightBarButtonItem = item3;#pragma mark - 设置多个按钮self.navigationItem.leftBarButtonItems = @[item1, item2];self.navigationItem.rightBarButtonItems = @[item1, item2];


      设置导航栏的标题为UI控件 
      #pragma mark - 设置标题为UI控件// 创建一个buttonUIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];[btn setTitle:@"绿色控制器" forState:UIControlStateNormal];// 设置文字颜色[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];// 监听点击事件[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];// 设置给标题的titleView属性self.navigationItem.titleView = btn;
      注意:
      如果不是导航控制器的根控制器,在push之后会在左侧显示返回按钮,点击返回按钮会跳转到上一级控制器
      如果在这个非根控制器内设置了左侧按钮,那么返回功能就需要通过代码的方式实现。
      /**如果在控制器内部指定了左侧的返回按钮,那么在点击按钮的时候就不会将控制器弹出,需要自己手动实现弹出功能。*/UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(popSelf)];self.navigationItem.leftBarButtonItem = item;
      ```objc#pragma mark - 手动实现出栈方法- (void)popSelf {    // 将当前控制器出栈    [self.navigationController popViewControllerAnimated:YES];}```


    0 0
    原创粉丝点击