1. 程式人生 > >論UITableViewController被status bar覆蓋的問題(IOS SDK 8.3)

論UITableViewController被status bar覆蓋的問題(IOS SDK 8.3)

通常我們做一個UITableViewController(以下簡稱tvc)直接開啟,在沒有NavigationController的時候都會是這樣
覆蓋了狀態列的tvc

問題是,你要加一個UINavigationController嗎?

還是像別人一樣不用UITableViewControler而是在main storyboard 裡面造一個UIViewController + UITableView 下移20px?

告訴你我最近實現的一個方法:

#define BARITEMHEIGHT 64
- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect
frame = self.view.frame; frame.size.height = BARITEMHEIGHT; UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:frame]; UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Active group"]; UIBarButtonItem *leftbutton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self
action:@selector(actionDown)]; UIBarButtonItem *rightbutton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(actionCancel)]; [navigationBar pushNavigationItem:navigationItem animated:YES]; [navigationItem setLeftBarButtonItem:leftbutton]; [navigationItem setRightBarButtonItem:rightbutton]; self
.tableView.tableHeaderView = navigationBar; } -(void) actionDown { [self dismissViewControllerAnimated:YES completion:^{ // balabalabala... }]; } -(void) actionCancel { [self dismissViewControllerAnimated:YES completion:nil]; }

省事羅!
完成加入navigatebar

  1. 由於加入的navigatebar是tableView的一部分,在手指滑動的時候這個navigatebar是會隨著移動的,如果希望navigatebar不動,那麼,就要把
- (void)viewDidLoad
{
...
}

裡面的內容去掉
然後,在

-(void)viewDidAppear:(BOOL)animated
{
}

加上類似的內容,致於為什麼加這裡,可能是因為viewDidLoad的時候某些內部的控制元件還沒有準備好吧

-(void)viewDidAppear:(BOOL)animated
{
    CGRect frame = self.view.frame;
    frame.origin.y += BARITEMHEIGHT;
    frame.size.height  -= BARITEMHEIGHT;
    self.view.frame = frame;

    frame.origin.y = 0;
    frame.size.height = BARITEMHEIGHT;

    UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:frame];

    UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Active appointment"];
    UIBarButtonItem *leftbutton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(actionDown)];
    UIBarButtonItem *rightbutton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(actionCancel)];
    [navigationBar pushNavigationItem:navigationItem animated:YES];
    [navigationItem setLeftBarButtonItem:leftbutton];
    [navigationItem setRightBarButtonItem:rightbutton];

    [self.view.superview addSubview:navigationBar];
}

感謝一下我吧!
轉載不要忘了發我的連結