您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS UITableView 背景色與背景圖片設定技巧

編輯:iOS 開發綜合

修改 header 與 footer 背景色

過去常見做法是自訂 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];

這種方式下,不論列表有多少行,背景圖片都會維持相同樣式。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved