回答
希望從UITableViewDelegate
協議此方法將讓你開始:
的Objective-C:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
if (section == integerRepresentingYourSectionOfInterest)
[headerView setBackgroundColor:[UIColor redColor]];
else
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
斯威夫特:
func tableView(_ tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView!
{
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
if (section == integerRepresentingYourSectionOfInterest) {
headerView.backgroundColor = UIColor.redColor()
} else {
headerView.backgroundColor = UIColor.clearColor()
}
return headerView
}
更新2017:
斯威夫特3:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 30))
if (section == integerRepresentingYourSectionOfInterest) {
headerView.backgroundColor = UIColor.red
} else {
headerView.backgroundColor = UIColor.clear
}
return headerView
}
更換[UIColor redColor]
與哪個UIColor
你想。您也可以調整headerView
的尺寸。
它也可以幫助您使用self.tableView.sectionHeaderHeight調整節標題大小,否則,您可能無法看到顯示的文本節標題 – 2010-01-23 20:56:55
與`[UIColor xxxColor]`工作正常`但是當我嘗試一個客戶湯姆顏色就像我可以從Photoshop中獲得的顏色(所以使用'UIColor red:green:blue:alpha:`,它只是白色。難道我做錯了什麼? – 2013-04-09 01:16:15
發佈一個單獨的問題,我們會盡力幫助。包含源代碼。 – 2013-04-09 07:29:14
以下是如何更改文字顏色。
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
label.text = @"Section Header Text Here";
label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];
下面介紹如何在頭視圖中添加圖像:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
UIImageView *headerImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"top-gery-bar.png"]] autorelease];
headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 30);
[headerView addSubview:headerImage];
return headerView;
}
不要忘了從委託添加這段代碼或您的觀點將被切斷或出現在桌子後面有些情況下,相對於您的視圖/標籤的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
在UITableViewHeaderFooterView上設置背景顏色已被棄用。請使用contentView.backgroundColor
代替。因爲iOS的6.0
[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];
該解決方案的偉大工程:
如果你想自定義顏色的頭部,你可以做到這一點。
如果你不希望創建一個自定義視圖,你也可以改變這樣的顏色(需要iOS 6):
-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
UIView* content = castView.contentView;
UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
content.backgroundColor = color;
}
}
這是一個老問題,但我認爲答案需要被更新。
此方法不涉及定義和創建自己的自定義視圖。 在iOS 6中和了,你可以很容易地改變背景顏色和界定
部分委託方法
例如文本顏色:
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor blackColor];
// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor whiteColor]];
// Another way to set the background color
// Note: does not preserve gradient effect of original header
// header.contentView.backgroundColor = [UIColor blackColor];
}
從我的文章摘自這裏: https://happyteamlabs.com/blog/ios-how-to-customize-table-view-header-and-footer-colors/
斯威夫特3
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
view.tintColor = UIColor.red
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
}
我有一個項目在iOS 7.x中使用靜態表格視圖單元格。 willDisplayHeaderView不會觸發。然而,這種方法效果好:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSLog(@"%s", __FUNCTION__);
CGRect headerFrame = CGRectMake(x, y, w, h);
UIView *headerView = [[UIView alloc] initWithFrame:headerFrame];
headerView.backgroundColor = [UIColor blackColor];
-(void) tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view
forSection:(NSInteger)section
{
if ([view isKindOfClass: [UITableViewHeaderFooterView class]])
{
UITableViewHeaderFooterView *castView = (UITableViewHeaderFooterView *) view;
UIView *content = castView.contentView;
UIColor *color = [UIColor whiteColor]; // substitute your color here
content.backgroundColor = color;
[castView.textLabel setTextColor:[UIColor blackColor]];
}
}
在iOS系統7.0.4我創建了它自己的XIB自定義標題。之前沒有提到任何工作。它必須是UITableViewHeaderFooterView的子類才能與dequeueReusableHeaderFooterViewWithIdentifier:
一起使用,並且類似於背景色非常固執。所以最後我添加了名爲customBackgroudView的UIView(可以使用代碼或IB),然後設置它的backgroundColor屬性。在layoutSubviews中:我將該視圖的框架設置爲界限。 它與iOS 7一起工作,不會出現任何故障。
// in MyTableHeaderView.xib drop an UIView at top of the first child of the owner
// first child becomes contentView
// in MyTableHeaderView.h
@property (nonatomic, weak) IBOutlet UIView * customBackgroundView;
// in MyTableHeaderView.m
-(void)layoutSubviews;
{
[super layoutSubviews];
self.customBackgroundView.frame = self.bounds;
}
// if you don't have XIB/use IB, put in the initializer:
-(id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
...
UIView * customBackgroundView = [[UIView alloc] init];
[self.contentView addSubview:customBackgroundView];
_customBackgroundView = customBackgroundView;
...
}
// in MyTableViewController.m
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
MyTableHeaderView * header = [self.tableView
dequeueReusableHeaderFooterViewWithIdentifier:@"MyTableHeaderView"];
header.customBackgroundView.backgroundColor = [UIColor redColor];
return header;
}
設置部分區域的背景和文本顏色:(感謝William Jockusch
和Dj S
)
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
castView.contentView.backgroundColor = [UIColor grayColor];
[castView.textLabel setTextColor:[UIColor grayColor]];
}
}
對於iOS8上(測試版)和斯威夫特選擇你想要的RGB顏色和嘗試這個辦法:
override func tableView(tableView: UITableView!, viewForHeaderInSection section: Int) -> UIView! {
var header :UITableViewHeaderFooterView = UITableViewHeaderFooterView()
header.contentView.backgroundColor = UIColor(red: 254.0/255.0, green: 190.0/255.0, blue: 127.0/255.0, alpha: 1)
return header
}
(「覆蓋「是因爲我在我的項目中使用UITableViewController而不是正常的UIViewController,但它不是強制更改節頭顏色)
您的頭文本仍然可以看到。 請注意,您將需要調整節頭高度。
祝你好運。
我認爲這段代碼並不是那麼糟糕。
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier(MyHeaderView.reuseIdentifier) as MyHeaderView
let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.whiteColor()
headerView.backgroundView = backgroundView
headerView.textLabel.text = "hello"
return headerView
}
以下解決方案適用於雨燕1.2與iOS 8+
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
// This changes the header background
view.tintColor = UIColor.blueColor()
// Gets the header view as a UITableViewHeaderFooterView and changes the text colour
var headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
headerView.textLabel.textColor = UIColor.redColor()
}
我知道它的回答,爲了以防萬一,在斯威夫特使用以下
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let tableViewWidth = self.tableView.bounds
let headerView = UIView(frame: CGRectMake(0, 0, tableViewWidth.size.width, self.tableView.sectionHeaderHeight))
headerView.backgroundColor = UIColor.greenColor()
return headerView
}
隨着RubyMotion/RedPotion,粘貼到你的r TableScreen:
def tableView(_, willDisplayHeaderView: view, forSection: section)
view.textLabel.textColor = rmq.color.your_text_color
view.contentView.backgroundColor = rmq.color.your_background_color
end
工程就像一個魅力!
只要改變頭部視圖的層的顏色
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease]; headerView.layer.backgroundColor = [UIColor clearColor].CGColor }
SWIFT 2
我是能夠成功地改變與加入的模糊效果的部分的背景顏色(這是非常酷) 。輕鬆更改部分的背景色:
- 首先去故事板,並選擇表視圖
- 轉到屬性檢查器
- 列表項
- 向下滾動查看
- 更改「背景」
然後,對於模糊效果,添加操作的代碼:
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
// This is the blur effect
let blurEffect = UIBlurEffect(style: .Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
// Gets the header view as a UITableViewHeaderFooterView and changes the text colour and adds above blur effect
let headerView: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
headerView.textLabel!.textColor = UIColor.darkGrayColor()
headerView.textLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 13)
headerView.tintColor = .groupTableViewBackgroundColor()
headerView.backgroundView = blurEffectView
}
如果有人需要迅速,保持標題:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0,y: 0,width: self.tableView.frame.width, height: 30))
view.backgroundColor = UIColor.redColor()
let label = UILabel(frame: CGRect(x: 15,y: 5,width: 200,height: 25))
label.text = self.tableView(tableView, titleForHeaderInSection: section)
view.addSubview(label)
return view
}
的iOS 8+
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
tableView.tableHeaderView?.backgroundColor = UIColor.blue()
}
基於@Dj的回答,用斯威夫特3.本適用於iOS 10大。
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
// Background color
view.tintColor = UIColor.black
// Text Color
let headerView = view as! UITableViewHeaderFooterView
headerView.textLabel?.textColor = UIColor.white
}
我通過控制檯日誌從Xcode收到消息
[TableView]設置背景顏色 UITableViewHeaderFooterView已被棄用。請將您想要的背景色設置爲自定義 UIView,然後改爲backgroundView 屬性。
然後我只是創建一個新的UIView並將其作爲HeaderView的背景。 不是一個好的解決方案,但很容易,因爲Xcode說。
在我的情況下,它的工作是這樣的:
let headerIdentifier = "HeaderIdentifier"
let header = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: headerIdentifier)
header.contentView.backgroundColor = UIColor.white
雖然func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
也能發揮作用,你可以達致這沒有實現另一個委託方法。 在你的方法func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
,你可以使用view.contentView.backgroundColor = UIColor.white
而不是view.backgroundView?.backgroundColor = UIColor.white
這是行不通的。 (我知道backgroundView
是可選的,但即使是在那裏,這不是沒有實現willDisplayHeaderView
使用UIAppearance你可以改變它像這樣在你的應用程序的所有頭沃金:
UITableViewHeaderFooterView.appearance() .backgroundColor = theme.subViewBackgroundColor
- 1. 更改UITableView節標題的顏色
- 2. 更改UITableView標題色調顏色
- 3. 顏色更改爲標題中的UITableView
- 4. 更改UITableView標題的背景顏色
- 5. 更改UITableView部分標題的顏色
- 6. 在UITableview中更改節標題的顏色
- 7. 更改GridViewColumn標題顏色
- 8. 更改顏色標題欄
- 9. 自動更改UITableView顏色?
- 10. UITableView如何更改顏色?
- 11. 如何更新的章節標題,而不UITableView的reloadData顏色
- 12. UITableView - 節標題。如何更改文字?
- 13. 更改節標題標題(普通桌面視圖)的背景顏色
- 14. visual C#更改標籤標題顏色
- 15. Nimbus更改標籤標題的顏色
- 16. 更改treeview節點顏色
- 17. 更改標題的顏色RichTextBox VB.NET
- 18. 更改操作欄標題顏色
- 19. 更改DatePicker的標題文字顏色
- 20. 更改標題UIActivityController的文本顏色
- 21. 如何更改objectchoicefield標題的顏色
- 22. 更改UIButton的默認標題顏色
- 23. 更改標題顏色和背景
- 24. HighCharts - 動態更改軸標題顏色
- 25. 更改WPF標題欄背景顏色
- 26. 更改導航標題背景顏色
- 27. Android更改XML中的標題顏色
- 28. 更改顏色條標題的位置
- 29. flexdashboard - 更改標題欄顏色
- 30. 如何更改datagridView標題顏色
我真的很喜歡編輯RE較新的iOS版本 – Suz 2015-08-21 17:15:29