Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableViewHeaderFooterView:无法更改背景颜色_Iphone_Ios_Xcode_Uitableview - Fatal编程技术网

Iphone UITableViewHeaderFooterView:无法更改背景颜色

Iphone UITableViewHeaderFooterView:无法更改背景颜色,iphone,ios,xcode,uitableview,Iphone,Ios,Xcode,Uitableview,我正在尝试更改UITableViewHeaderFooterView的背景色。尽管视图正在显示,但背景颜色仍为默认颜色。我从xcode得到一个日志,上面写着: 已在UITableViewHeaderFooterView上设置背景色 不赞成。请改用contentView.backgroundColor 但是,以下选项均无效: myTableViewHeaderFooterView.contentView.backgroundColor = [UIColor blackColor]; myTable

我正在尝试更改UITableViewHeaderFooterView的背景色。尽管视图正在显示,但背景颜色仍为默认颜色。我从xcode得到一个日志,上面写着:

已在UITableViewHeaderFooterView上设置背景色 不赞成。请改用contentView.backgroundColor

但是,以下选项均无效:

myTableViewHeaderFooterView.contentView.backgroundColor = [UIColor blackColor];
myTableViewHeaderFooterView.backgroundView.backgroundColor = [UIColor blackColor];
myTableViewHeaderFooterView.backgroundColor = [UIColor blackColor];
我还尝试更改xib文件中视图的背景色


有什么建议吗?谢谢。

您应该使用myTableViewHeaderFooterView.tintColor,或者为myTableViewHeaderFooterView.backgroundView指定自定义背景视图。

在iOS 7
contentView中。backgroundColor
对我有效,
tintColor
没有

   headerView.contentView.backgroundColor = [UIColor blackColor];
- (void)setupBackgroundColor:(UIColor *) color {
    UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
    bgView.backgroundColor = color;
    self.backgroundView = bgView;
}
虽然
clearColor
不适合我,但我找到的解决方案是将
backgroundView
属性设置为透明图像。也许它会帮助某人:

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

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

我试着让它看起来像一条链子,它对我很有效

[[UIView appearanceWhenContainedIn:[UITableViewHeaderFooterView class], [UITableView class], nil] 
         setBackgroundColor: [UIColor.grayColor]];
iOS 8、9、10、11。。。 设置任何颜色(使用任何alpha)的唯一方法是使用
backgroundView

self.contentView.backgroundColor = [UIColor whiteColor];
Swift

self.backgroundView=ui视图(框架:self.bounds)
self.backgroundView.backgroundColor=UIColor(白色:0.5,alpha:0.5)
Obj-C

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

评论回复

  • 这些其他选项都不能可靠地工作(尽管有以下评论)

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

  • 使用
    UIColor(白色:0.5,alpha:0.5)
    backgroundView.alpha=0.5

    (当然,任何颜色都可以)

  • 使用XIB时,将根视图设为
    UITableViewHeaderFooterView
    ,并以编程方式关联
    backgroundView

    向以下机构登记:

    tableView.register(UINib(nibName:“视图”,bundle:nil),
    forHeaderFooterViewReuseIdentifier:“标题”)
    
    加载:

    override func tableView(u tableView:UITableView,
    视图标题部分:Int)->UIView?{
    如果让标题=
    tableView.dequeueReusableHeaderFooterView(带有标识符:“header”){
    let backgroundView=UIView(帧:header.bounds)
    backgroundView.backgroundColor=UIColor(白色:0.5,alpha:0.5)
    header.backgroundView=背景视图
    回流集管
    }
    归零
    }
    


► 请在上查找此解决方案,并在上查找其他详细信息。

请确保为您的
UITableViewHeaderFooterView
设置了
contentView
的背景色:

self.contentView.backgroundColor = [UIColor whiteColor];

然后它就可以工作了。

对于清晰的颜色,我使用

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

我觉得不错。

在iOS9上headerView.backgroundView.backgroundColor对我有效:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    TableViewHeader *headerView = (TableViewHeader *)[super tableView:tableView viewForHeaderInSection:section];
    headerView.backgroundView.backgroundColor = [UIColor redColor];
}
在iOS8上,我使用的是headerView.contentView.backgroundColor,没有任何问题,但现在在iOS 9上,我遇到了一个奇怪的问题,使得背景色无法填满整个单元格。所以我尝试了
headerView.backgroundColor
,我从OP得到了同样的错误

已在UITableViewHeaderFooterView上设置背景色 不赞成。请改用contentView.backgroundColor


因此,现在通过使用
headerView.backgroundView.backgroundColor

将背景视图设置为清晰的颜色,可以在没有警告的情况下很好地处理可见的标题。 如果滚动表格以在底部显示标题,则此解决方案将失败

我的表格只包含标题,没有任何单元格

如果是,则确保“表节标题”视图的背景色为默认值

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


通过该设置,您可以更改
containerView
的背景色。

如果您使用
xib
文件创建了
UITableViewHeaderFooterView
的自定义子类,则应覆盖
setBackgroundColor
。让它空着

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

}

这将解决您的问题。

Swift:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView {
    var headerView: TableViewHeader = super.tableView(tableView, viewForHeaderInSection: section) as! TableViewHeader
    headerView.backgroundView.backgroundColor = UIColor.redColor()
}

创建UIView并设置背景色,然后将其设置为self.backgroundView

- (void)setupBackgroundColor:(UIColor *) color {
    UIView *bgView = [[UIView alloc] initWithFrame:self.bounds];
    bgView.backgroundColor = color;
    self.backgroundView = bgView;
}

对我来说,我尝试了上面提到的一切,但仍然得到了警告 “已不推荐在UITableViewHeaderFooterView上设置背景色。请改用contentView.backgroundColor。”然后我尝试了以下操作: 在xib文件中,标题视图的背景色被选择为“清除颜色”,而不是“默认颜色”,一旦我将其更改为“默认颜色”,警告就会消失。


我觉得有必要分享我的经验。 我的这段代码在iOS 10和iOS 11上运行良好
headerView?.contentView.backgroundColor=.lightGray

然后我突然决定为iOS 9部署应用程序,因为有一些设备(iPad mini,一些老一代不会更新到9以外的任何操作系统)-唯一适用于所有iOS 9、10和11的解决方案是为标题定义一个基本视图,然后包含所有其他标题子视图,从故事板将其连接起来,并设置该基本视图的
backgroundColor

你会想要b
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    if let headerView = view as? UITableViewHeaderFooterView {
        headerView.contentView.backgroundColor = .clear // Does not work 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).

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
        }
    }
}
MySectionHeaderView.appearance().forceBackgroundColor = .red
MySectionHeaderView.appearance(whenContainedInInstancesOf: [MyOtherClass.self]).forceBackgroundColor = .red
extension UITableViewHeaderFooterView {

    open override var backgroundColor: UIColor? {
        get {
            return self.backgroundColor
        }
        set {
            let bgView = UIView()
            bgView.backgroundColor = newValue
            backgroundView = bgView
        }
    }
}
headerView.backgroundColor = .red
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = tv.dequeueReusableHeaderFooterView(withIdentifier: "MyHeaderView")
    headerView.backgroundColor = .red // <- here
    return headerView
}
class MyHeaderView: UITableViewHeaderFooterView {

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .red // <- here
    }
}
let bgView = UIView()
bgView.backgroundColor = UIColor.clear
backgroundView = bgView
backgroundColor = UIColor.clear
contentView.backgroundColor = UIColor.clear