Jack Guo
Jack Guo

Reputation: 4694

Change grouped UITableView footer textLabel color

How to change the text color of the built-in textLabel of UITablewViewHeaderFooterView? I tried

override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    guard let footer = tableView.dequeueReusableHeaderFooterView(withIdentifier: footerId) else {
        return nil
    }
    footer.textLabel?.textColor = UIColor.blue
    return footer
}

but nothing happens. My table view is of .grouped style, if that matters

Upvotes: 0

Views: 289

Answers (1)

CodingMeSwiftly
CodingMeSwiftly

Reputation: 3261

You'll have to override tableView:willDisplayFooter:forSection for that. Somewhere between tableView:viewForFooterInSection: and the latter, UIKit resets the textColor. Setting it in willDisplayFooter makes sure you change it last and therefore win :).

Update

If you register a custom UITableViewHeaderFooterView subclass for your footer views, you may also override willMove(toWindow:) in that class and perform your changes there.

Another alternative is to set the attributedText property of the textLabel. UIKit won't mess with the color in that.

Upvotes: 1

Related Questions