【问题标题】:How to set background of a view (UITableview) with dynamic cells?如何使用动态单元格设置视图(UITableview)的背景?
【发布时间】:2017-01-25 13:58:27
【问题描述】:

我正在尝试为 UITableViewCell 占用的视图设置背景颜色。我拖了一个出口并有一个渐变层,我返回它

extension CAGradientLayer{
func viewGradient(topColour:UIColor,bottomColour : UIColor) -> CAGradientLayer{
   let gradientColor: [CGColor] = [topColour.cgColor, bottomColour.cgColor]
   let gradientLocations: [Float] = [0.0/1.0]
   let gradientLayer: CAGradientLayer = CAGradientLayer()
   gradientLayer.colors = gradientColor
   gradientLayer.locations = gradientLocations as [NSNumber]?

   return gradientLayer
  }
}

我在视图控制器的viewDidLoad 方法中访问此方法

 let bottomColour = UIColor(red: (0.018), green: (0.38), blue: (0.64), alpha: 1.0)
 let topColour = UIColor(red: (0.13), green: (0.63), blue: (0.67), alpha: 0.7)
 let background = CAGradientLayer().viewGradient(topColour:topColour,bottomColour : bottomColour)
 background.frame = self.view.bounds
 self.view.layer.insertSublayer(background, at: 0)
 myMenu.layer.insertSublayer(background, at: 0)

我的表格视图中有动态单元格,随着单元格数量的增加,视图会向下滑动,我必须滚动才能看到我的单元格。我有空格。有什么方法可以将整个滚动区域设置为具有我的背景渐变颜色还是我错过了什么?

Here is the screenshot of the images showing my exact problem

【问题讨论】:

  • UITableView 有一个 backgroundView 属性,用于在其后面放置一个固定视图。

标签: ios swift uitableview uiview background


【解决方案1】:

如上所述,UITableView 有一个 backgroundView 属性。

在您的viewDidLoad 方法中,保持您的background 常量声明原样,然后在下面添加:

self.tableView.backgroundView = background

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多