论导航栏隐藏的简单快捷的正确姿势

写法一 简单快捷

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
  self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

生命周期函数里写两行代码就去 Do something..........
缺点:当二级页面pop回来时 一级页面的导航栏右侧会有黑色的一块很不雅观 如下图

未命名.gif

写法二 换种写法

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

缺点 pop顺畅了 但是切换底部Tabbar上的item时 导致隐藏了导航栏的控制器上下跳动 如下图

未命名22.gif

写法三 正确写法

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

解决了以上两个小问题 暂时未发现缺点


记录TableView reloadData不执行的一个小坑 坑坑哇哇 什么玩意😝
应用场景
控制器中 有个数组属性
重写该数组的set方法 去reloadData 当外界每次给控制器的数组赋值时 刷新控制器中的tableView
TableView的创建通过懒加载创建的写法如下

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGRectGetHeight(self.view.frame) - 66 - iPhoneXStateBarHeight - 0) style: UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WordsCell class] forCellReuseIdentifier:cellID];
        _tableView.backgroundColor = HWColor(246, 246, 246);
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    }
    return _tableView;
}
- (void)setDataArr:(NSArray *)dataArr
{
    _dataArr = dataArr;
    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
}

发现赋值空数组时cellForRowAtIndexPath 没执行
最后尝试了下改成下边的就好了,创建视图的时候就添加.原因未知,其他地方保持上边的写法并没有问题:

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGRectGetHeight(self.view.frame) - 66 - iPhoneXStateBarHeight - 0) style: UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[WordsCell class] forCellReuseIdentifier:cellID];
        _tableView.backgroundColor = HWColor(246, 246, 246);
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
       [self.view addSubView:_tableView];
    }
    return _tableView;
}

鉴于能力有限水平一般 理解有误之处 望不吝指出 深表感谢

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容