改變UITableView的headerView、footerView背景顏色

問題

改變UITableView的header、footer背景顏色,這是個很常見的問題。以前知道的通常作法是,經過實現tableView: viewForHeaderInSection:返回一個自定義的View,裏面什麼都不填,只設背景顏色。可是今天發現一個更簡潔的作法。函數

更簡潔的方法

對於iOS 6及之後的系統,實現這個新的delegate函數便可:ui

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { view.tintColor = [UIColor clearColor]; }

還能夠改變文字的顏色:spa

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view; [footer.textLabel setTextColor:[UIColor whiteColor]]; }

錯誤的嘗試

寫這篇文章的目的,主要是想記錄兩種錯誤的嘗試。
當看到這個Delegate函數時,第一反應是想固然地這樣作:code

錯誤嘗試1

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { view.backgroundColor = [UIColor clearColor]; }

這樣作是無效的,不管對什麼顏色都無效。it

錯誤嘗試2

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section { UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view; footer.contentView.backgroundColor = [UIColor redColor]; }

這樣作設成不透明的顏色就沒問題。但設成clearColor,看到的仍是灰色。io



文/戴倉薯(簡書做者) 原文連接:http://www.jianshu.com/p/bfb237f5c20c 著做權歸做者全部,轉載請聯繫做者得到受權,並標註「簡書做者」。
相關文章
相關標籤/搜索