UIViewController自带的NavigationViewController不显示的问题

    问题: 妹子最近在写代码的时候想使用一下UIViewController自带的navigationVIewController的时候发现无论在navigation中写入什么代码,界面都没有显示,通过分层图发现界面上压根就没有这个navigationBar的容器啊,哪里去了,哪里去了?

    例子:先贴出妹子错误的示范:

    if (self.navigationController) { // 去掉这一层也是错误的示范哦

        self.navigationItem.title = @"姨吗爱买";

        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@""] style:UIBarButtonItemStylePlain target:self action:@selector(selector)];

    }

  

   原因:查阅了navigationController的属性,发现没有在本界面设置某一个属性就可以使得navigationBar出来,而问题的根本在这里

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = navigationController;

    只有在APPdelegate中添加了这句话,并且保证self.window.rootViewController = navigationController;才会创建出navigationBar;

   所以如果是简单的demo的话是可以直接这样使用的,但是一个复杂的demo中是需要用到各种视图控制器,比如tabBar这样两者就会发生冲突,面对这种情况下,小伙伴们要乖乖的自定义一个navigationViewController,添加到UITableView中,然后自定义这个navigationViewController,代码如下

   例如:

@property (nonatomic, strong) UIView *tableHeaderView;

- (UITableView *)tableView

{

    if (!_tableView) {

        _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];

        _tableView.tableHeaderView = self.tableHeaderView;

    }

    return _tableView;

}

- (UIView *)tableHeaderView

{

    if (!_tableHeaderView) {

        _tableHeaderView = [[UIView alloc]init];

        _tableHeaderView.frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, 100);

        _tableHeaderView.backgroundColor = [UIColor clearColor];

    }

    return _tableHeaderView;

}

   虽然写完以后觉得很简单,但是之前一直找不到原因啊,所以记录下来希望能帮到需要的小伙伴

posted @ 2016-09-13 10:36  萌蠢的驴子  阅读(3279)  评论(0编辑  收藏  举报