修改UITableViewStyleGrouped形式的tableview的背景色 此博文包含圖片 (2013-09-10 11:11:57)轉載▼ 分類: IOS筆記 在使用UITable


    在使用UITableViewStylePlain形式的tableview時,很容易就修改了背景色,而在使用UITableViewStyleGrouped形式的tableview時,怎麼都修改不了背景色。原因:

在iOS6中,對於 UITableViewStyleGrouped類型的UITableView,通過直接修改繼承自UIViewbackgroundColor屬性的方法來設置UITableView的背景色無效。

比如,在AppDelegate中設置窗口的顏色爲淡黃色

self.window.backgroundColor = [UIColor colorWithRed:1.00f green:0.91f blue:0.82f alpha:1.00f];

在一個UIViewController的viewDidLoad方法中增加一個UITableView,設置其backgroundColor爲透明色。

UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style: UITableViewStyleGrouped];
tableView.dataSource = self;
tableView.delegate = self;
tableView.backgroundColor = [UIColor clearColor];
[self.view addSubview:tableView];

 

那麼在iOS5及之前版本的模擬器上,運行的效果如下:

因爲UITableView的背景色設爲了[UIColor clearColor],所以tableView的背景色爲UIWindow的顏色。
但是在iOS6模擬器和運行iOS6設備上的顯示效果如下:

此時UITableView的背景色爲默認的灰色,我們通過backgroundColor設置的背景色無效。

這個問題只在UITableViewStyleGrouped類型的UITableView中出現,UITableViewStylePlain類型的tableView沒有這個問題,因爲Group類型的TableView有個backgroundView,而plain類型的TableView沒有(backgroundView屬性爲nil),目前看來,這可能因爲backgroundView在中間擋住了背景色,這是否iOS6的bug還待確認。關於backgroundView,還可以參考下這裏iPad Table backgroundView

目前對於這個問題的解決方法是將Group類型的tableView的backgroundView設爲一個新的空白View或簡單的設置爲nil.如下

tableView.backgroundView = [[UIView alloc]init];
tableView.backgroundColor = [UIColor clearColor];

tableView.backgroundView = nil;
tableView.backgroundColor = [UIColor clearColor];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章