我需要實現在屏幕上完全集中顯示標籤的能力,其他都不可見,但這需要在UITableView
中完成。該設置是UISpiltViewController
,其中有一個UITableViewController
用於詳細視圖控制器,並且當左側沒有選擇任何項目時,我想顯示一條消息,指出在右側,以及當用戶選擇標籤應立即消失並顯示桌子。 (就像郵件應用程序一樣)創建tableHeaderView始終填充可見表格的框架?
我已經有了這個設置,它工作正常,但由於某種原因,它並不總是保持居中在屏幕上,這不是一個很好的解決方案 - 有一些小奇怪例如,當旋轉發生時,您可以部分看到桌面的頂部。我只是創建一個UILabel
,將它的frame
設置爲填充可見區域,然後將表tableHeaderView
設置爲該標籤,最後禁用滾動表格的功能。在旋轉時,必須更新frame
以再次填充可見區域。這就是奇怪發生的地方,因爲直到旋轉完成後它纔會更新。
我的問題是,有什麼更好的方法來實現這種行爲?有什麼方法可以防止在旋轉後更新框架,是否可以使用自動佈局tableHeaderView
?
//Setting the tableHeaderView
self.tableView.tableHeaderView = self.label;
//Creating the UILabel
- (UILabel *)label {
_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width,
self.tableView.bounds.size.height
- self.navigationController.navigationBar.frame.size.height
- self.tabBarController.tabBar.frame.size.height
- MIN([UIApplication sharedApplication].statusBarFrame.size.height,
[UIApplication sharedApplication].statusBarFrame.size.width))];
_label.text = @"Nothing Selected";
_label.textAlignment = NSTextAlignmentCenter;
_label.backgroundColor = self.tableView.backgroundColor;
return _label;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
if (_label) {
_label.frame = CGRectMake(0, 0, self.tableView.bounds.size.width,
self.tableView.bounds.size.height
- self.navigationController.navigationBar.frame.size.height
- self.tabBarController.tabBar.frame.size.height
- MIN([UIApplication sharedApplication].statusBarFrame.size.height,
[UIApplication sharedApplication].statusBarFrame.size.width));
self.tableView.tableHeaderView = nil;
self.tableView.tableHeaderView = _label;
}
}
對不起,我應該特別說明。是的,它是一個'UITableViewController',我肯定會更喜歡這樣。 :) – Joey 2014-10-02 21:51:55
@Joey,爲什麼?我不明白爲什麼這麼多人堅持使用UITableViewController。使用UIViewController無法輕鬆完成這件事,這並不會讓你感到太多,它使得你想要做的更加困難。 – rdelmar 2014-10-02 22:02:53