jamfer
V2EX  ›  问与答

[swift3] 请问 UItableviewcell 中的 image 怎么设定边距啊?

  •  
  •   jamfer · Aug 11, 2017 · 2056 views
    This topic created in 3196 days ago, the information mentioned may be changed or developed.
    cell.imageView.image = UIImage(named:"avatar")
    

    我给某个 cell 加了一张图片,但是图片是顶着 cell 上下边儿的。我希望能留一些 padding 出来(也就是内边距)

    请问我该怎么写?

    我试了

    cell.imageView.frame.origin.y += 10
    cell.imageView.frame.size.height -= 10
    

    不行,没效果

    7 replies    2017-08-11 23:26:31 +08:00
    elgae
        1
    elgae  
       Aug 11, 2017
    最最最优雅的做法,子类化 `UITableViewCell` ,重写 `-layoutSubviews` 方法。

    ```
    - (void)layoutSubviews {
    [super layoutSubviews];
    //在这里随便处置你的 imageView
    }
    ```
    jamfer
        2
    jamfer  
    OP
       Aug 11, 2017
    @elgae 问题是我只有一个 cell 需要加图,感觉用 UITableViewCell 有点儿麻烦... 还有其他方法么?
    elgae
        3
    elgae  
       Aug 11, 2017
    @jamfer 哪里麻烦?
    另外一种思路是:重绘图片,重绘时留白边。
    你可以把页面截图发出来看一下。
    jamfer
        4
    jamfer  
    OP
       Aug 11, 2017
    @elgae 就是苹果手机点「设置」进去后第一眼看到的样子,上面是一个头像和名字(还有一行小字),下面是一堆导航 menu。

    那个头像就是有 padding 的
    elgae
        5
    elgae  
       Aug 11, 2017
    @jamfer 两种方法都行
    acumen
        6
    acumen  
       Aug 11, 2017 via iPhone
    曲线救国啊。不能给 imageview 和 cell 之间加间距吗?
    BoBoy
        7
    BoBoy  
       Aug 11, 2017
    现在还有不自定义 cell 的吗?

    请自定义 cell,谢谢!

    LZ 刚入门吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5916 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 06:21 · PVG 14:21 · LAX 23:21 · JFK 02:21
    ♥ Do have faith in what you're doing.