【问题标题】:UIImageView not shaping into a proper circle shape in UITableViewCellUIImageView 没有在 UITableViewCell 中塑造成正确的圆形
【发布时间】:2020-03-27 18:28:07
【问题描述】:

我在 TableViewCell 中有一个 UIImageView,我想在其中将 Image View 显示为圆形,并在网上做了一些研究,但是 UIImageView strong> 改为转换为圆形矩形类型。

这是示例问题:

如您所见,图像是圆形的,但它更像是一个矩形圆圈,而不是我想要实现的实际圆圈。

故事板对象放置

这是我尝试过的操作

  • UIImageViewheightwidth 设置为相同的值
  • 内容模式:方面填充(已在情节提要中启用)
  • cellForRowAt 函数 - 见下面的代码
cell.backgroundImageView.layer.borderWidth = 1
cell.backgroundImageView.layer.masksToBounds = true
cell.backgroundImageView.layer.cornerRadius = cell.backgroundImageView.frame.width / 2

感谢 Stackoverflow 社区是否可以帮助我解决这个神秘问题并显示一个正常的圆圈。

编辑:添加约束

<constraint firstAttribute="width" secondItem="SN7-KQ-bLs" secondAttribute="height" multiplier="1:1" id="fnk-DG-TK8"/>

<constraint firstItem="VVM-Vf-iwJ" firstAttribute="trailing" secondItem="IYt-ky-1cl" secondAttribute="trailing" id="0f0-6E-N9y"/>
                                        <constraint firstItem="VVM-Vf-iwJ" firstAttribute="top" secondItem="C1t-o2-7NZ" secondAttribute="topMargin" id="0oA-se-a9X"/>
                                        <constraint firstItem="SN7-KQ-bLs" firstAttribute="leading" secondItem="C1t-o2-7NZ" secondAttribute="leadingMargin" id="953-xq-W2Y"/>
                                        <constraint firstItem="IYt-ky-1cl" firstAttribute="top" secondItem="VVM-Vf-iwJ" secondAttribute="bottom" constant="-3" id="BFj-p7-RM6"/>
                                        <constraint firstItem="VVM-Vf-iwJ" firstAttribute="leading" secondItem="C1t-o2-7NZ" secondAttribute="leadingMargin" constant="54" id="hVv-nQ-bAl"/>
                                        <constraint firstAttribute="trailingMargin" secondItem="SN7-KQ-bLs" secondAttribute="trailing" constant="218" id="k47-df-sZ3"/>
                                        <constraint firstItem="SN7-KQ-bLs" firstAttribute="top" secondItem="VVM-Vf-iwJ" secondAttribute="top" id="sbF-ni-mJE"/>
                                        <constraint firstItem="VVM-Vf-iwJ" firstAttribute="leading" secondItem="IYt-ky-1cl" secondAttribute="leading" id="uoy-hv-n5V"/>
                                        <constraint firstItem="VVM-Vf-iwJ" firstAttribute="trailing" secondItem="C1t-o2-7NZ" secondAttribute="trailingMargin" id="xr8-93-737"/>
                                        <constraint firstAttribute="bottomMargin" secondItem="SN7-KQ-bLs" secondAttribute="bottom" constant="7" id="ysw-uw-sXi"/>

【问题讨论】:

  • 您不应该在cellForRowAt 中这样做。您应该改为在 init 上执行此操作,并且应该在单元格的 layoutSubviews 方法中应用 cornerRadius。另外,检查您的约束是否被破坏。如果您也显示您的限制条件,将会有所帮助。
  • 是否添加了纵横比限制?
  • 我认为您可以修复图像的高度和宽度,例如。每个 70,然后添加一个宽度/2 的角半径就可以了。
  • 我在 TableViewCell 类中添加了一个新的 Override 函数,cornerRadius 被移动并包含在其中。我添加了一个纵横比约束(编辑后 - 这是第一个约束)。我已经尝试将高度和宽度都固定为 70,因为角半径会将宽度除以 2。同样的问题仍然存在。

标签: swift xcode uiimageview uistoryboard


【解决方案1】:
override func layoutSubviews() {
    super.layoutSubviews()

    cell.backgroundImageView.layer.borderWidth = 1
    cell.backgroundImageView.layer.masksToBounds = true
    cell.backgroundImageView.layer.cornerRadius = cell.backgroundImageView.frame.width / 2
 }

在你的 tableviewcell 类中试试这个

【讨论】:

    【解决方案2】:

    转到你的 uitableViewCell 课程并使用下面的代码,它对我有用,也对你有用。

     override func awakeFromNib() {
        super.awakeFromNib()
        backGroundImageView.layer.cornerRadius = backGroundImageView.frame.width / 2
        backGroundImageView.clipsToBounds = true
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      • 2015-08-29
      • 2014-05-03
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多