2013-10-02 33 views
54

我有UITabbarController其中UINavigationController在裏面。我有一個UIView的子類,我在navController中指定爲viewUIViewController。這是非常標準的東西,對吧?這就是我要做的事爲什麼UIViewController在UINavigationBar下擴展,而UITableViewController沒有?

_productCategoryView = [[ProductCategoryView alloc] initWithFrame:self.view.frame]; 
self.view = _productCategoryView; 

view有一個UITableView作爲subView

_productCategoryTableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain]; 
_productCategoryTableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
_productCategoryTableView.backgroundColor = [UIColor clearColor]; 
[self addSubview:_productCategoryTableView]; 

要進行調試的緣故,我在視圖中設置self.backgroundColor = [UIColor blueColor]

tableView的上述初始化中,人們可能會認爲視圖和表格的frame是相同的。但是,當我在iOS 7中運行時,視圖的原點設置在UINavigationBar的後面。這是可以理解的,因爲我在UINavigationController的子類中設置了self.navigationBar.translucent = YES;。但我不明白的是,桌子坐在navBar的下方?它不應該也從(0, 0)開始,它落後於navBar?請參閱下面的屏幕截圖Scenario 1。請注意後面navBar

Scenario 1

藍色色調現在,我push另一viewController導航堆棧上,只需使用[self.navigationController pushViewController.....]。再次我有一個自定義UIView其中tableView。不過,我也有這個表上面的UILabel,再次調試,我給它一個redColor。這一次,我設置了標籤的origin是因爲幾乎相同的觀點的

CGRect boundsInset = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(10, 10, 10, 10)); 

CGSize textSize = [_titleLabel.text sizeWithFont:_titleLabel.font 
           constrainedToSize:CGSizeMake(boundsInset.size.width, MAXFLOAT) 
            lineBreakMode:NSLineBreakByWordWrapping]; 
printSize(textSize); 
_titleLabel.frame = CGRectMake(boundsInset.origin.x, 
           boundsInset.origin.y, 
           boundsInset.size.width, 
           textSize.height); 

因此,通過上述邏輯去,標籤應該是可見的,對不對?但這次不是。這次標籤是在navBar後面。

Scenario - 2

通知,後面的Navbar紅色色調。

我真的想一直對齊navBar下面的子視圖。我的問題是

1. How is the tableView offset by 64pixels (height of nav + status bar in iOS 7) automatically, even though it's frame is same as the view's?

2. Why does that not happen in the second view?

+0

您是否正在使用Xcode 5和iOS7 SDK構建? –

回答

128

默認的UITableViewController的意見iOS7自動插頁,使它們不啓動導航欄/狀態欄的下方。這是通過Interface Builder中UITableViewController的屬性檢查器選項卡上的「調整滾動視圖插圖」設置或通過UIViewController的setAutomaticallyAdjustsScrollViewInsets:方法設置的控制器。

對於UIViewController的內容,如果您不希望其視圖的內容在頂部/底部條形圖下擴展,則可以使用界面構建器中的頂部條形圖/底部條形圖條下的延伸邊線。這可以通過edgesForExtendedLayout屬性訪問。

+48

謝謝@Greg。設置'self.edgesForExtendedLayout = UIRectEdgeNone;'做了訣竅。 – unspokenblabber

+0

謝謝。尋址edgesForExtendedLayout完成解決了我的uitableview-as-a-subview-of-navcontroller佈局問題;在過去8個月中花了數不清的時間。 – ObjectiveTC

+1

使用navigationController進行push轉換時,我發現應用於導航欄的「延遲」顏色處理。是什麼導致這種情況以及如何刪除它? – ArdenDev

89
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.edgesForExtendedLayout = UIRectEdgeNone; 
} 

斯威夫特2:

self.edgesForExtendedLayout = UIRectEdge.None 

斯威夫特3:

self.edgesForExtendedLayout = [] 
+0

正確,謝謝! –

+1

最有用的答案。 – yujean

+0

設置'self.edgesForExtendedLayout = []'更改'UINavigationBar'顏色。 – Hemang

9

@的Gank的答案是正確的,但要做到這一點,最好的地方是在UINavigationControllerDelegate(如果有的話) :

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) { 
    viewController.edgesForExtendedLayout = UIRectEdge.None 
} 
+2

這可以避免自定義超級視圖控制器。非常酷。 – Jaybo

+0

@Jaybo謝謝,是的,你把代碼放在哪裏會有很大的影響。 –

相關問題