iPhone: Show modal UITableViewController with Navigation bar

前端 未结 6 1031
温柔的废话
温柔的废话 2021-01-30 00:10

I am showing a modal view which is a UITableViewController class. For some reason it won\'t show the navigation bar when I show it. Here is my code:



        
6条回答
  •  囚心锁ツ
    2021-01-30 00:41

    On iOS 7 and you just want a navigation bar on your modal view controller to show a title and some buttons? Try this magic in your UITableViewController:

    // in the .h
    @property (strong) UINavigationBar* navigationBar;
    
    //in the .m
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.navigationItem.title = @"Awesome";
        self.navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectZero];
        [self.view addSubview:_navigationBar];
        [self.navigationBar pushNavigationItem:self.navigationItem animated:NO];
    }
    
    -(void)layoutNavigationBar{
        self.navigationBar.frame = CGRectMake(0, self.tableView.contentOffset.y, self.tableView.frame.size.width, self.topLayoutGuide.length + 44);
        self.tableView.contentInset = UIEdgeInsetsMake(self.navigationBar.frame.size.height, 0, 0, 0);
    }
    
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
        //no need to call super
        [self layoutNavigationBar];
    }
    
    -(void)viewDidLayoutSubviews{
        [super viewDidLayoutSubviews];
        [self layoutNavigationBar];
    }
    

提交回复
热议问题