2015-01-09 67 views
5

我使用UITableView代表方法viewForHeaderInSection在我的UITableView中提供了節標題。viewForHeaderInSection autolayout - 針腳寬度

我最初創建這樣一個觀點:

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 50)]; 

然後使用自動佈局,然後返回headerView

我是我不想明確指定headerView大小的問題添加一些子視圖。我想用Autolayout將左邊的&右邊緣固定到視圖的寬度。這是問題所在,我沒有在Autolayout代碼中使用superview。

使用上面的代碼,意味着標題視圖不會自動循環旋轉。旋轉後必須重新加載tableview。

任何想法如何設置headerView將其邊緣固定到tableview?

感謝

回答

2

從我的測試,這個答案here,從方法自動的原點設置爲(0, 0)返回UIView,其高度設置爲設置的寬度從-tableView: heightForHeaderInSection:,其寬度返回的值UITableView

我能夠將控件添加到該UIView甚至沒有在init方法中指定任何特定的大小自動佈局。

這裏是我的代碼來創建頭視圖:

self.headerView = [[UIView alloc] init]; 

這裏就是我躺在了頭視圖中的控件代碼:

- (void)layoutControls { 
    [self.headerView addSubview:self.segmentedControl]; 
    [self.headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(margin)-[control]-(margin)-|" 
                      options:0 
                      metrics:@{@"margin": @(self.segmentedControlLeftRightMargin)} 
                       views:@{@"control": self.segmentedControl}]]; 
    [self.headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(margin)-[control(==height)]" 
                      options:0 
                      metrics:@{@"margin": @(self.segmentedControlTopMargin), 
                         @"height": @(self.segmentedControlHeight)} 
                       views:@{@"control": self.segmentedControl}]]; 

    [self.headerView addSubview:self.searchBar]; 
    [self.headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(margin)-[control]-(margin)-|" 
                      options:0 
                      metrics:@{@"margin": @(self.searchBarLeftRightMargin)} 
                       views:@{@"control": self.searchBar}]]; 
    [self.headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[control1]-(margin1)-[control2]-(margin2)-|" 
                      options:0 
                      metrics:@{@"margin1": @(self.segmentedControlBottomMargin), 
                         @"margin2": @(self.searchBarBottomMargin)} 
                       views:@{@"control1": self.segmentedControl, 
                         @"control2": self.searchBar}]]; 

} 

下面是對UITableViewDelgate協議的方法:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    // The hard-coded values are accurate for my controls, but you might need more advanced logic 
    return 44.0f + self.segmentedControlBottomMargin + 44.0f + self.searchBarBottomMargin; 
} 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    return self.headerView; 
} 
+0

謝謝,我學到了東西,返回一個zerorect視圖使用正確的完整大小,所以不需要使用tableview.bounds,但是這在旋轉時不正確。你必須重新加載tableview。我猜測沒有超級視圖來貼近邊緣。 – Darren

+0

是的,它似乎被視爲「超出了自動佈局的界限」。當我在過去需要這種行爲時,我實際上已經對UITableViewCell進行了子類化,並使用它來代替節頭。如果實際使用段,它可能只是行0(零)的單元格。這樣,你** **得到旋轉支持。 – mbm29414

+0

這實際上不是一個壞主意。實際上,當表格滾動時,也會停止屏幕標題停留在屏幕上。 – Darren

相關問題