【发布时间】:2015-02-05 10:34:06
【问题描述】:
我在 IB 中创建了一个带有标签的 UIView:6。我在 tableView 中使用它。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
// create background view
var backgroundView = cell.contentView.viewWithTag(6) as UIView!
backgroundView.layer.cornerRadius = 10
backgroundView.layer.masksToBounds = true
// create gradient layer
let gradient : CAGradientLayer = CAGradientLayer()
// create color array
let arrayColors: [AnyObject] = [
UIColor (red: 33/255, green: 33/255, blue: 39/255, alpha: 1).CGColor,
UIColor (red: 24/255, green: 24/255, blue: 28/255, alpha: 1).CGColor]
// set gradient frame bounds to match view bounds
gradient.frame = backgroundView.bounds
// set gradient's color array
gradient.colors = arrayColors
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.locations = [0.1, 0.9]
gradient.endPoint = CGPoint(x: 1, y: 1)
// replace base layer with gradient layer
backgroundView.layer.insertSublayer(gradient, atIndex: 0)
问题是,虽然 backgroundView 调整为 Autolayout,但渐变层却没有,正如我所期望的那样:
gradient.frame = backgroundView.bounds
我在 TableView 单元格中找不到任何适用于 UIView 的答案。
问题:在 TableView 中强制应用到 UIView 的图层使用自动布局调整大小的正确代码是什么?
【问题讨论】:
标签: ios uitableview autolayout cagradientlayer