UITableView - 섹션 헤더 색상 변경
UITableView에서 섹션 헤더의 색상을 변경하려면 어떻게 해야 합니까?
편집: iOS 6 이상에서는 DJ-S가 제공하는 답변을 고려해야 합니다.승인된 답변이 오래되었습니다.
오래된 질문입니다만, 답변을 갱신할 필요가 있다고 생각합니다.
이 방법에서는 사용자 정의 보기를 정의하고 만들지 않습니다.iOS 6 이상에서는, 다음과 같이 정의하면, 배경색과 텍스트색을 간단하게 변경할 수 있습니다.
-(void)tableView:(UITableView *)tableView
willDisplayHeaderView:(UIView *)view
forSection:(NSInteger)section
섹션 위임법
예를 들어 다음과 같습니다.
- (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/4
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){
view.tintColor = UIColor.red
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = UIColor.white
}
은 ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★UITableViewDelegate과 같이 시작할 수 있습니다.
목표-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.
텍스트 색상을 변경하는 방법은 다음과 같습니다.
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];
커스텀 컬러의 헤더를 원하는 경우 이 작업을 수행할 수 있습니다.이 솔루션은 iOS 6.0 이후부터 잘 작동합니다.
목표 C:
[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];
신속:
UITableViewHeaderFooterView.appearance().tintColor = .white
다음 솔루션은 iOS 8+가 설치된 Swift 1.2에서 작동합니다.
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()
}
UITableView는 되었습니다.HeaderFooterView 。★★★★★★를 사용해 .contentView.backgroundColor★★★★★★ 。
main.storyboard에서 약 2초 안에 할 수 있습니다.
- 테이블 뷰 선택
- 속성 검사기로 이동
- 목록 항목
- 아래로 스크롤하여 소제목 보기
- "배경" 변경
딜러가 이 코드를 추가하는 것을 잊지 마십시오.그렇지 않으면 뷰/라벨 높이에 따라 뷰가 잘리거나 표 뒤에 표시될 수 있습니다.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
사용자 정의 보기를 만들지 않으려면 다음과 같이 색상을 변경할 수도 있습니다(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;
}
}
Swift 5 +
»willDisplayHeaderView ★★★
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
//For Header Background Color
view.tintColor = .black
// For Header Text Color
let header = view as! UITableViewHeaderFooterView
header.textLabel?.textColor = .white
}
도움이 되시길 바랍니다:]
영역의 to use at an hergan area).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]];
}
}
스위프트 4
UITableView 섹션의 헤더 뷰 배경색, 텍스트 레이블 색상 및 글꼴을 변경하려면 단순히 재정의하십시오.willDisplayHeaderView테이블 뷰에 대해 다음과 같이 설명합니다.
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.backgroundView?.backgroundColor = .white
header.textLabel?.textColor = .black
header.textLabel?.font = UIFont(name: "Helvetica-Bold", size: 14)
}
이것은 나에게 완벽하게 작동했습니다. 당신에게도 도움이 되기를 바랍니다!
헤더 뷰에 이미지를 추가하는 방법은 다음과 같습니다.
- (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;
}
iOS8(베타) 및 Swift의 경우 원하는 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
}
(프로젝트에서 일반 UIViewController가 아닌 UITableViewController를 사용하고 있기 때문에 "오버라이드"가 존재하지만 섹션 헤더 색상 변경은 필수가 아닙니다.)
헤더의 텍스트는 계속 표시됩니다.섹션 헤더 높이를 조정해야 합니다.
행운을 빌어요.
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
}
Swift 4는 매우 간단합니다.이것을 클래스에 추가하고 필요에 따라 색상을 설정하기만 하면 됩니다.
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.backgroundColor = UIColor(red: 0.094, green: 0.239, blue: 0.424, alpha: 1.0)
}
또는 단순한 색상의 경우
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.backgroundColor = UIColor.white
}
Swift 5용으로 갱신
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor(red: 0.094, green: 0.239, blue: 0.424, alpha: 1.0)
}
또는 단순한 색상의 경우
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor.white
}
2시간을 허비해도 위의 어느 것도 효과가 없습니다.이것이 해결책입니다.제 경우 커스텀 뷰였지만 스토리보드나 뷰의 awake From Nib에서 변경할 수 없습니다.
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.contentView.backgroundColor = .white
}
만약을 위해, In Swift는 다음을 사용합니다.
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
}
iOS 8 이상
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
tableView.tableHeaderView?.backgroundColor = UIColor.blue()
}
Swift 3을 사용하여 @Dj S 답변을 기반으로 합니다.이것은 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
}
iOS 7.x에서 정적 테이블 뷰 셀을 사용하는 프로젝트가 있습니다.HeaderView는 기동하지 않는다.단, 이 방법은 정상적으로 동작합니다.
- (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]];
}
}
이 코드는 나쁘지 않은 것 같아요.
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
}
iOS 7.0.4에서는 자체 XIB를 사용하여 커스텀 헤더를 만들었습니다.을 사용법[ UITable View ]를 선택합니다.HeaderFooterView를 하여 HeaderFooterView를 합니다.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;
}
헤더 뷰의 레이어 색상을 변경하기만 하면 됩니다.
- (UIView*) tableView: (UITableView*) tableviewForHeaderInSection: (NSInteger) 섹션 보기{UIView *headerView = [[[UIView allocate] initWithFrame :CGRectMake(0, 0, tableView).bounds.size.width, 30)] 자동 리스];
headerView.layer.backgroundColor = [ UICollor clearColor ] 。CG Collor}
신속한 지원이 필요한 경우 제목 유지:
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
}
콘솔 로그를 통해 Xcode에서 메시지를 받았습니다.
[TableView] UITableView 배경색 설정HeaderFooterView는 폐지되었습니다.원하는 배경색을 가진 커스텀 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){
let tableHeader = view as! UITableViewHeaderFooterView
tableHeader.backgroundView?.backgroundColor = UIColor.white
}
커스텀 헤더뷰를 사용하는 경우:
class YourCustomHeaderFooterView: UITableViewHeaderFooterView {
override func awakeFromNib() {
super.awakeFromNib()
self.contentView.backgroundColor = .white //Or any color you want
}
}
언급URL : https://stackoverflow.com/questions/813068/uitableview-change-section-header-color
'programing' 카테고리의 다른 글
| 목록에서 목록으로 캐스팅하기 위한 더 짧은 구문? (0) | 2023.04.09 |
|---|---|
| Bash 스크립트에서 특정 상황이 발생했을 때 스크립트 전체를 종료하려면 어떻게 해야 합니까? (0) | 2023.04.09 |
| Excel VBA에서 변경된 셀의 이전 값을 얻으려면 어떻게 해야 합니까? (0) | 2023.04.09 |
| 셸 스크립트에서 하위 문자열 하나를 다른 문자열로 바꿉니다. (0) | 2023.04.09 |
| 배치 스크립트루프 (0) | 2023.04.09 |
