生活随笔
收集整理的這篇文章主要介紹了
iOS- 如何改变section header
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
希望這個(gè)從UITableViewDelegate協(xié)議里得到的方法可以對(duì)你有所幫助:
- (
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;
}
使用任何你喜歡UIColor代替[UIColor redColor]。你可能還希望調(diào)整headerView的尺寸。
DoctorG 這是改變文本顏色的方法:
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];
whyoz 不要忘記從委托添加這段代碼,否則在某些情況下視圖將被切斷或者出現(xiàn)在table后面,相對(duì)于視圖/標(biāo)簽的高度。
- (
CGFloat )tableView:(
UITableView *)tableView heightForHeaderInSection:(
NSInteger )section
{
return 30 ;
}
Leszek ?arna 如果你想自定義header顏色,可以這樣做:
[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]] ;
這個(gè)方法在iOS 6.0.以上都很好用。
Maulik 這是在標(biāo)題視圖添加圖片的方法:
- (
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;
}
William Jockusch 如果你不想建立自定義視圖,你也可以這樣改變顏色(需要在iOS6里):
-(
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. ]; content
.backgroundColor = color;}
}
Dj S 這是常見(jiàn)的問(wèn)題,我認(rèn)為答案需要更新一下。 這個(gè)方法不涉及定義和創(chuàng)建自定義視圖。在iOS 6以上,你可以通過(guò)以下方法輕松改變背景色和文本色:
- (void) tableView:(UITableView *) tableView willDisplayHeaderView:(UIView *) view forSection:(NSInteger ) section
委托方法 例如:
- (
void )tableView:(
UITableView *)tableView willDisplayHeaderView:(
UIView *)view forSection:(
NSInteger )section
{view
.tintColor = [
UIColor blackColor];UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;[header
.textLabel setTextColor:[
UIColor whiteColor]];
}
orbv 通過(guò)UITableViewHeaderFooterView設(shè)置背景色的方法已經(jīng)被廢棄了。請(qǐng)用contentView.backgroundColor代替。
轉(zhuǎn)載于:https://www.cnblogs.com/mcj-coding/p/3696923.html
總結(jié)
以上是生活随笔 為你收集整理的iOS- 如何改变section header 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。