【问题标题】:Error for setting the backgroundColor of a UITableHeaderFooterView设置 UITableHeaderFooterView 的 backgroundColor 时出错
【发布时间】:2019-04-15 21:04:32
【问题描述】:

我的表格视图有一个自定义的 Header 类,每当使用它时,我都会不断收到此错误:

[TableView] 在 UITableViewHeaderFooterView 上设置背景颜色已被弃用。请改为将具有所需背景颜色的自定义 UIView 设置为 backgroundView 属性。

以下是我尝试过的一些事情:

我在 UITableViewHeaderFooterView 子类的 awakeFromNib() 中添加了一个 backgroundView:

override func awakeFromNib() {
    super.awakeFromNib()
    self.backgroundView = UIView(frame: self.bounds)
    self.backgroundView?.backgroundColor = UIColor(red:0.84, green:0.47, blue:0.97, alpha:1.0)
}

我在定义标题部分的 tableView 函数中添加了一个 backgroundView:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: Header.identifier) as? Header {

        // Extraneous Code

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

        // Extraneous code
    }
    return UIView()
}

在做完这些之后,我仍然得到错误。

【问题讨论】:

  • 你正试图返回一个新的干净的 UIView()。您的观点,您在上面设置的,与此无关。改为在 viewForHeader 中返回您的 headerView。

标签: ios swift uitableview uiview


【解决方案1】:

我能够防止错误出现。我是这样做的:

转到页眉的 Xib 文件,背景颜色选择默认值。然后,在 viewForHeaderInSection 函数中,添加:

let backgroundView = UIView(frame: CGRect.zero)
backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
headerView.backgroundView = backgroundView

我知道我在问题中提到了它,但关键是将 xib 背景颜色设置为“默认”。

【讨论】:

    【解决方案2】:

    检查你必须设置headerView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)

    在您的 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 函数中。

    此外,这足以添加背景颜色

     func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
         let backgroundView = UIView(frame: CGRect.zero)
         backgroundView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
         return backgroundView
    }
    

    【讨论】:

      【解决方案3】:

      如果你可以继承UITableViewHeaderFooterView,你可以试试这个方法。

      class SampleHeaderView: UITableViewHeaderFooterView {
          override func awakeFromNib() {
              super.awakeFromNib()
      
              let bgView = UIView(color: .black)
      
              addSubview(bgView)
      
              bgView.translatesAutoresizingMaskIntoConstraints = false
              let layoutAttributes: [NSLayoutAttribute] = [.top, .leading, .bottom, .trailing]
              layoutAttributes.forEach { attribute in
                  addConstraint(NSLayoutConstraint(item: bgView,
                                                   attribute: attribute,
                                                   relatedBy: .equal,
                                                   toItem: self,
                                                   attribute: attribute,
                                                   multiplier: 1,
                                                   constant: 0.0))
              }
          }
      }
      
      extension UIView {
          convenience init(color: UIColor) {
              self.init(frame: .zero)
              backgroundColor = color
          }
      }
      

      【讨论】:

        【解决方案4】:

        使用名称 backgroundView 时,您不会收到以下错误:

        1) 'backgroundView' 的模糊使用

        2) 无法覆盖可变属性“backgroundView”

        3) 不能用存储的属性“backgroundView”覆盖

        因为您没有添加backgroundView,所以您使用的是UITableViewHeaderFooterView 拥有的那个。

        对于awakeFromNib

        override func awakeFromNib() {
            super.awakeFromNib()
            let bgView = UIView(frame: self.bounds)
            bgView.backgroundColor = UIColor(red:0.84, green:0.47, blue:0.97, alpha:1.0)
            self.addSubview(bgView)
        }
        

        对于viewForHeaderInSection

        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            if let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: Header.identifier) as? Header {
        
            // Extraneous Code
        
                let bgView = UIView(frame: headerView.bounds)
                bgView.backgroundColor = UIColor(white: 0.5, alpha: 0.5)
                headerView.addSubview(bgView)
        
                return headerView
                // Extraneous code
            }
            return UIView()
        }
        

        您只需将名称 backgroundView 更改为其他名称。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-06
          • 2021-06-06
          • 1970-01-01
          相关资源
          最近更新 更多