UITableViewHeaderFooterView: Unable to change background color

前端 未结 20 2096
误落风尘
误落风尘 2020-12-23 08:38

I\'m trying to change the background color of UITableViewHeaderFooterView. Although the view is appearing, the background color remains the default color. I\'m getting a log

相关标签:
20条回答
  • 2020-12-23 09:21

    Swift:

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
        var headerView: TableViewHeader = super.tableView(tableView, viewForHeaderInSection: section) as! TableViewHeader
        headerView.backgroundView.backgroundColor = UIColor.redColor()
    }
    
    0 讨论(0)
  • 2020-12-23 09:23

    If you are customising a section header cell with Storyboard/Nib, then make sure the background color is default for the "Table Section Header" view.

    And if you subclass UITableViewHeaderFooterView, and using nib, then what you have to do is to create a IBOutlet for the content view, and name it eg. containerView. This is not to be confused with contentView, which is parent of this container view.

    With that setup, you change the background color of containerView instead.

    0 讨论(0)
  • 2020-12-23 09:25

    maybe because the backgroundView doesn't exist

    override func draw(_ rect: CGRect){
        // Drawing code
        let view = UIView()
        view.frame = rect
        self.backgroundView = view
        self.backgroundView?.backgroundColor = UIColor.yourColorHere
    }
    

    that work for me.

    0 讨论(0)
  • 2020-12-23 09:26

    iOS 12, Swift 5. If you are willing (or trying!) to use an appearance proxy, the following solution works:

    1. Subclass UITableViewHeaderFooterView and expose your own appearance proxy setting.
    final class MySectionHeaderView: UITableViewHeaderFooterView {
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    
        override init(reuseIdentifier: String?) {
            super.init(reuseIdentifier: reuseIdentifier)
        }
    
        @objc dynamic var forceBackgroundColor: UIColor? {
            get { return self.contentView.backgroundColor }
            set(color) {
                self.contentView.backgroundColor = color
                // if your color is not opaque, adjust backgroundView as well
                self.backgroundView?.backgroundColor = .clear
            }
        }
    }
    
    1. Set the appearance proxy at your desired granularity.
    MySectionHeaderView.appearance().forceBackgroundColor = .red
    

    or

    MySectionHeaderView.appearance(whenContainedInInstancesOf: [MyOtherClass.self]).forceBackgroundColor = .red
    
    0 讨论(0)
  • 2020-12-23 09:29

    You should either use myTableViewHeaderFooterView.tintColor, or assign a custom background view to myTableViewHeaderFooterView.backgroundView.

    0 讨论(0)
  • 2020-12-23 09:29

    On iOS9 headerView.backgroundView.backgroundColor worked for me:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        TableViewHeader *headerView = (TableViewHeader *)[super tableView:tableView viewForHeaderInSection:section];
        headerView.backgroundView.backgroundColor = [UIColor redColor];
    }
    

    On iOS8 I was using headerView.contentView.backgroundColor without problems, but now with iOS 9, I was getting a weird issue that made the background color not fill the whole space of the cell. So I tried just headerView.backgroundColor and I got the same error from the OP.

    Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead.

    So now everything works great and without warnings by using headerView.backgroundView.backgroundColor

    0 讨论(0)
提交回复
热议问题