最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift设置表格视图的单元格的分割线的左对齐或右边填充距离

Swift crifan 2387浏览 0评论

希望实现:

分割线,要么左边顶格对齐,要么距离右边有个填充距离

之前设置过separatorInset的left为0,但是不起效果

swift uitableview cell separator left inset

objective c – iOS 8 UITableView separator inset 0 not working – Stack Overflow

看了下说明:

extension UIView {
    /*
     -layoutMargins returns a set of insets from the edge of the view’s bounds that denote a default spacing for laying out content.
     If preservesSuperviewLayoutMargins is YES, margins cascade down the view tree, adjusting for geometry offsets, so that setting the left value of layoutMargins on a superview will affect the left value of layoutMargins for subviews positioned close to the left edge of their superview’s bounds
     If your view subclass uses layoutMargins in its layout or drawing, override -layoutMarginsDidChange in order to refresh your view if the margins change.
     */
    @available(iOS 8.0, *)
    public var layoutMargins: UIEdgeInsets

去设置:

        //make separator left align to edge
        self.tableView.separatorInset = UIEdgeInsetsZero
        self.tableView.layoutMargins = UIEdgeInsetsZero

效果:

没变化。

再去试试:

        //make separator left align to edge
        self.tableView.separatorInset = UIEdgeInsetsZero
        self.tableView.layoutMargins = UIEdgeInsetsZero
        self.tableView.preservesSuperviewLayoutMargins = true

效果:

没变化。

结果:

        //make separator left align to edge
        cell.layoutMargins = UIEdgeInsetsZero
//        cell.layoutMargins.left = 0
        cell.preservesSuperviewLayoutMargins = false

也没变化。

最后用:

        //make separator left align to edge
        cell.separatorInset = UIEdgeInsetsZero
        cell.layoutMargins.left = 0
        cell.preservesSuperviewLayoutMargins = false

或:

        //make separator left align to edge
        cell.separatorInset = UIEdgeInsetsZero
        cell.layoutMargins = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false

都达到了效果:

分隔符完全左右顶格显示了:

转载请注明:在路上 » [已解决]swift设置表格视图的单元格的分割线的左对齐或右边填充距离

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.233 seconds, using 22.15MB memory