首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS UITableView Section Header 文字居中顯示的實現方法

     編輯:程式設計綜合問答

在 iOS 中,tableView:titleForHeaderInSection: 返回的標題文字默認左對齊,若需居中顯示,必須實現 tableView:viewForHeaderInSection: 返回自定義視圖。

方案一:返回自定義 UILabel(推薦)

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.textAlignment = NSTextAlignmentCenter; // 關鍵:居中對齊
    headerLabel.font = [UIFont boldSystemFontOfSize:14];
    headerLabel.textColor = [UIColor whiteColor];
    
    switch (section) {
        case 0: headerLabel.text = NSLocalizedString(@"more_titlehead_one", nil); break;
        case 1: headerLabel.text = NSLocalizedString(@"more_titlehead_two", nil); break;
        default: headerLabel.text = NSLocalizedString(@"more_titlehead_three", nil); break;
    }
    
    return headerLabel;
}

// 設置 Header 高度(可選)
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30.0; // 根據字體大小調整
}

方案二:使用系統樣式但修改外觀(iOS 6+ 外觀代理)

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 全局設置 UITableViewHeaderFooterView 外觀
    [[UITableViewHeaderFooterView appearance] setTintColor:[UIColor whiteColor]];
    // 注意:無法直接通過 appearance 設置 textAlignment
}

此方案無法直接實現文字居中,不推薦用於此需求。

方案三:自定義 Header View 類別(複雜佈局)

// CustomHeaderView.h
@interface CustomHeaderView : UITableViewHeaderFooterView
@property (nonatomic, strong) UILabel *titleLabel;
@end

// CustomHeaderView.m
@implementation CustomHeaderView
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self.contentView addSubview:self.titleLabel];
        [NSLayoutConstraint activateConstraints:@[
            [self.titleLabel.centerXAnchor constraintEqualToAnchor:self.contentView.centerXAnchor],
            [self.titleLabel.centerYAnchor constraintEqualToAnchor:self.contentView.centerYAnchor]
        ]];
    }
    return self;
}
@end

// 使用
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[CustomHeaderView class] forHeaderFooterViewReuseIdentifier:@"CustomHeader"];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CustomHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CustomHeader"];
    header.titleLabel.text = [self titleForSection:section];
    return header;
}

關鍵注意事項

  1. 實現 viewForHeaderInSection: 後,titleForHeaderInSection: 將被忽略
  2. 必須實現 heightForHeaderInSection: 返回合適高度,否則可能顯示異常
  3. iOS 6+ 建議使用 dequeueReusableHeaderFooterViewWithIdentifier: 復用 Header 視圖
  4. 背景色設置在 contentView.backgroundColor 或 Header 視圖的 backgroundView

Swift 版本示例

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let label = UILabel()
    label.textAlignment = .center
    label.font = .boldSystemFont(ofSize: 14)
    label.textColor = .white
    label.backgroundColor = .clear
    label.text = self.titleForSection(section)
    return label
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 30
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved