【问题标题】:Border in right side of UITableViewCell not visibleUITableViewCell 右侧的边框不可见
【发布时间】:2016-07-24 21:07:53
【问题描述】:

我正在尝试在表格视图单元格周围添加边框。以下是cellForRowAtIndexPath中的代码:

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.separatorInset.left = 20.0
        cell.separatorInset.right = 20.0
        cell.separatorInset.top = 20.0
        cell.separatorInset.bottom = 20.0
        cell.layer.borderWidth = 3
        cell.layer.cornerRadius = 20.0
        cell.layer.borderColor = UIColor.colorWithHexString("80CBC4").CGColor
        return cell
    }

边框适用于三个边,右侧除外:

【问题讨论】:

  • 已为单元格视图定义了widthheight 约束
  • 从我最近的回答中尝试解决方案...
  • 所以你想要的边框是顶部和底部的浅蓝绿色,左右两侧的半径为白色?此外,构建单元的方式也存在一些问题。例如。宽度指定为 574,我很确定您可以将所有布局代码移动到 IB
  • @user1349663 以编程方式,我只添加边框,我留下的宽度作为默​​认值。问题是我忘记为表格视图本身指定约束,而不是单元格视图。
  • @triple.s 听起来你已经明白了 :) 如果你不能在 IB 中这样做,我仍然会将所有单元格布局代码移动到自定义单元格类中,但这可能只是个人喜好。

标签: ios swift uitableview


【解决方案1】:

尝试将maskToBounds 设置为true 层属性的代码:

当此属性的值为true 时,Core Animation 会创建一个隐式剪切蒙版,该蒙版匹配图层的边界并包含任何角半径效果。如果还指定了掩码属性的值,则将两个掩码相乘以获得最终掩码值。 该属性的默认值为false

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.separatorInset.left = 20.0
        cell.separatorInset.right = 20.0
        cell.separatorInset.top = 20.0
        cell.separatorInset.bottom = 20.0
        cell.contentView.layer.borderWidth = 3
        cell.contentView.layer.cornerRadius = 20.0
        cell.contentView.layer.borderColor = UIColor.colorWithHexString("80CBC4").CGColor
        cell.contentView.layer.masksToBounds  = true
        return cell
}

【讨论】:

  • 还是一样的结果
  • 所以我猜单元格宽度等于屏幕宽度,对吧?
  • 是的。我在模拟器上添加了我的xib 和应用程序的另一个快照。
  • 好吧,我猜你的边框是存在的,但是在用户区域内是不可见的......尝试减小单元格宽度只是为了调试我的想法......
  • 问题是我缺少 tableView 而不是 cellView 的约束
【解决方案2】:

您可能已经指定了固定宽度约束。 您应该删除它并指定前导和尾随约束。

【讨论】:

  • 问题是我缺少 tableView 而不是 cellView 的约束
猜你喜欢
  • 2016-08-17
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 2022-11-16
  • 2010-10-21
  • 2021-05-10
  • 2022-12-10
  • 1970-01-01
相关资源
最近更新 更多