ios UITableViewHeaderFooterView:无法更改背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15604900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:51:14  来源:igfitidea点击:

UITableViewHeaderFooterView: Unable to change background color

iphoneiosxcodeuitableview

提问by Chun

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 from xcode saying:

我正在尝试更改 UITableViewHeaderFooterView 的背景颜色。尽管出现了视图,但背景颜色仍为默认颜色。我从 xcode 得到一个日志说:

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

不推荐在 UITableViewHeaderFooterView 上设置背景颜色。请改用 contentView.backgroundColor。

However, none of the following options work:

但是,以下选项均无效:

myTableViewHeaderFooterView.contentView.backgroundColor = [UIColor blackColor];
myTableViewHeaderFooterView.backgroundView.backgroundColor = [UIColor blackColor];
myTableViewHeaderFooterView.backgroundColor = [UIColor blackColor];

I've also tried changing the background color of the view in the xib file.

我还尝试更改了 xib 文件中视图的背景颜色。

Any suggestions? Thanks.

有什么建议?谢谢。

采纳答案by Matt

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

您应该使用 myTableViewHeaderFooterView.tintColor,或将自定义背景视图分配给 myTableViewHeaderFooterView.backgroundView。

回答by SwiftArchitect

iOS 8, 9, 10, 11...

iOS 8、9、10、11...

The only way to set any color (with any alpha) is to use backgroundView:

设置任何颜色(使用任何 alpha)的唯一方法是使用backgroundView

Swift

迅速

self.backgroundView = UIView(frame: self.bounds)
self.backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)

Obj-C

对象-C

self.backgroundView = ({
    UIView * view = [[UIView alloc] initWithFrame:self.bounds];
    view.backgroundColor = [UIColor colorWithWhite: 0.5 alpha:0.5];
    view;
    });


Responses to Comments

对评论的回应

  • None of these other options reliably work (despite the comments below)

    // self.contentView.backgroundColor = [UIColor clearColor];
    // self.backgroundColor = [UIColor clearColor];
    // self.tintColor = [UIColor clearColor];
    
  • the backgroundViewis resized automatically. (No need to add constraints)

  • Control alpha with UIColor(white: 0.5, alpha: 0.5)or backgroundView.alpha = 0.5.
    (of course, any color will do)

  • When using XIB, make root view a UITableViewHeaderFooterViewand associate the backgroundViewprogrammatically:

    Register with:

    tableView.register(UINib(nibName: "View", bundle: nil),
                       forHeaderFooterViewReuseIdentifier: "header")
    

    Load with:

    override func tableView(_ tableView: UITableView,
                            viewForHeaderInSection section: Int) -> UIView? {
        if let header =
            tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") {
            let backgroundView = UIView(frame: header.bounds)
            backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
            header.backgroundView = backgroundView
            return header
        }
        return nil
    }
    
  • 这些其他选项都不能可靠地工作(尽管下面有评论)

    // self.contentView.backgroundColor = [UIColor clearColor];
    // self.backgroundColor = [UIColor clearColor];
    // self.tintColor = [UIColor clearColor];
    
  • backgroundView会自动调整大小。(无需添加约束)

  • 使用UIColor(white: 0.5, alpha: 0.5)或控制 alpha backgroundView.alpha = 0.5
    (当然,任何颜色都可以)

  • 使用XIB 时,使根视图 aUITableViewHeaderFooterView并以backgroundView编程方式关联:

    注册:

    tableView.register(UINib(nibName: "View", bundle: nil),
                       forHeaderFooterViewReuseIdentifier: "header")
    

    加载:

    override func tableView(_ tableView: UITableView,
                            viewForHeaderInSection section: Int) -> UIView? {
        if let header =
            tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") {
            let backgroundView = UIView(frame: header.bounds)
            backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
            header.backgroundView = backgroundView
            return header
        }
        return nil
    }
    


Demo

演示

? replay animation

? 重播动画

? Find this solution on GitHuband additional details on Swift Recipes.

? 在GitHub 上找到此解决方案,在Swift Recipes找到更多详细信息。

回答by sash

In iOS 7 contentView.backgroundColorworked for me, tintColordid not.

在 iOS 7contentView.backgroundColor为我工作,tintColor没有。

   headerView.contentView.backgroundColor = [UIColor blackColor];

Though clearColordid not work for me, the solution I found is to set backgroundViewproperty to transparent image. Maybe it will help someone:

虽然clearColor对我不起作用,但我找到的解决方案是将backgroundView属性设置 为透明图像。也许它会帮助某人:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 1), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

headerView.backgroundView = [[UIImageView alloc] initWithImage:blank];

回答by Rudolf J

Make sure you set the backgroundColor of the contentViewfor your UITableViewHeaderFooterView:

请确保您设置的的的backgroundColorcontentViewUITableViewHeaderFooterView

self.contentView.backgroundColor = [UIColor whiteColor];

Then it will work.

然后它会起作用。

回答by AmJa

For me I tried everything stated above but still was getting the warning "Setting the background color on UITableViewHeaderFooterView has been deprecated. Please use contentView.backgroundColor instead." then I tried this: within the xib file the background color for header view was selected to clear color instead of default once I changed it to default the warning went away. was this

对我来说,我尝试了上述所有内容,但仍然收到警告“在 UITableViewHeaderFooterView 上设置背景颜色已被弃用。请改用 contentView.backgroundColor。” 然后我尝试了这个:在xib文件中,标题视图的背景颜色被选择为清除颜色而不是默认颜色,一旦我将其更改为默认值警告消失了。 这是

Changed to this

改成这个

回答by ChildhoodAndy

For a clear color, I use

对于清晰的颜色,我使用

self.contentView.backgroundColor = [UIColor clearColor];
self.backgroundView = [UIView new];
self.backgroundView.backgroundColor = [UIColor clearColor];

It seems fine to me.

对我来说似乎很好。

回答by Eneko Alonso

For solid background colors, setting the contentView.backgroundColorshould be enough:

对于纯色背景,设置contentView.backgroundColor应该就足够了:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.contentView.backgroundColor = .red // Works!
    }
}

For colors with transparency, including .clearcolor, this no longer works:

对于具有透明度的颜色,包括.clear颜色,这不再有效:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.contentView.backgroundColor = .clear // Does not work 
    }
}

For a full transparent section header, set the backgroundViewproperty to an empty view:

对于完全透明的部分标题,将backgroundView属性设置为空视图:

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.backgroundView = UIView() // Works!
    }
}

However, beware of possible side effects. Unless the table view is set to "Grouped", section headers will snap at the top when scrolling down. If the section headers are transparent, the cell content will be seen through, which might not look great.

但是,请注意可能的副作用。除非 table view 设置为“Grouped”,否则当向下滚动时,节标题将在顶部对齐。如果部分标题是透明的,则单元格内容将被看到,这可能看起来不太好。

Here, section headers have transparent background:

在这里,节标题具有透明背景:

enter image description here

在此处输入图片说明

To prevent this, it is better to set the background of the section header to a solid color (or gradient) matching the background of your table view or view controller.

为了防止这种情况,最好将部分标题的背景设置为与表视图或视图控制器的背景相匹配的纯色(或渐变)。

Here, section headers have a fully opaque gradient background:

在这里,部分标题具有完全不透明的渐变背景:

enter image description here

在此处输入图片说明

回答by Hosny

enter image description hereIn interface builder pull up your .xib, on the top element, in attribute inspector set the background color to default. Then go to the Content View and set the background color there (reference to https://github.com/jiecao-fm/SwiftTheme/issues/24).

在此处输入图片说明在界面构建器中,在顶部元素上拉出您的 .xib,在属性检查器中将背景颜色设置为默认值。然后转到内容视图并在那里设置背景颜色(参考https://github.com/jiecao-fm/SwiftTheme/issues/24)。

回答by samwize

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

如果您使用 Storyboard/Nib 自定义节标题单元格,请确保“表格节标题”视图的背景颜色是默认值

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

如果你子类化UITableViewHeaderFooterView,并使用nib,那么你要做的就是IBOutlet为内容视图创建一个,并将其命名为例如。containerView. 这不要与contentView,它是此容器视图的父级混淆。

With that setup, you change the background color of containerViewinstead.

使用该设置,您可以更改 的背景颜色containerView

回答by Voloda2

if you created a custom subclass of UITableViewHeaderFooterViewwith xibfile then you should override setBackgroundColor. Keep it empty.

如果您创建了一个UITableViewHeaderFooterViewwithxib文件的自定义子类,那么您应该覆盖setBackgroundColor. 让它空着。

-(void)setBackgroundColor:(UIColor *)backgroundColor {

}

And this will solve your problem.

这将解决您的问题。