過去常見做法是自訂 view 並回傳給 tableView:viewForHeaderInSection:。在 iOS 6 之後,可直接實作 willDisplayFooterView:forSection:,透過 tintColor 調整背景。
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
view.tintColor = [UIColor clearColor];
}
若要修改文字顏色,可把 view 轉型為 UITableViewHeaderFooterView,再設定 textLabel 的文字顏色。
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view;
[footer.textLabel setTextColor:[UIColor whiteColor]];
}
若想讓圖片像地磚一樣重複排列,可使用 colorWithPatternImage:。
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BackgroundImage"]]; cell.backgroundColor = [UIColor clearColor];
這種方式會讓背景隨滾動而移動,若內容高度超過圖片高度,會繼續顯示下一張重複圖片。
若希望捲動時背景保持不動,可建立 UIImageView 並指定給 tableView.backgroundView。
self.tableView.backgroundColor = [UIColor clearColor]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BackgroundImage"]]; self.tableView.backgroundView = imageView; cell.backgroundColor = [UIColor clearColor];
這種方式下,不論列表有多少行,背景圖片都會維持相同樣式。