UIImageView 没有在 UITableViewCell 中塑造成正确的圆形
UIImageView not shaping into a proper circle shape in UITableViewCell
我在 TableViewCell 中有一个 UIImageView,我想在其中将图像视图显示为一个圆圈,并在网上做了一些研究,但是 UIImageView 会转换为圆形矩形类型。
示例问题如下:
如您所见,图像是圆形的,但它更像是一个矩形圆圈,而不是我想要实现的实际圆圈。
故事板对象放置
这是我试过的方法
- 将 UIImageView 的 height 和 width 设置为相同的值
- 内容模式:纵横比填充(已在故事板中启用)
- cellForRowAt 函数 - 请参见下面的代码
cell.backgroundImageView.layer.borderWidth = 1
cell.backgroundImageView.layer.masksToBounds = true
cell.backgroundImageView.layer.cornerRadius = cell.backgroundImageView.frame.width / 2
感谢 Whosebug 社区可以帮助我解决这个神秘问题并显示一个正常的圆圈。
编辑:添加约束
<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"/>
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 中试试这个 class
转到您的 uitableViewCell
class 并使用下面的代码,它适用于我,也适用于您。
override func awakeFromNib() {
super.awakeFromNib()
backGroundImageView.layer.cornerRadius = backGroundImageView.frame.width / 2
backGroundImageView.clipsToBounds = true
}
我在 TableViewCell 中有一个 UIImageView,我想在其中将图像视图显示为一个圆圈,并在网上做了一些研究,但是 UIImageView 会转换为圆形矩形类型。
示例问题如下:
故事板对象放置
这是我试过的方法
- 将 UIImageView 的 height 和 width 设置为相同的值
- 内容模式:纵横比填充(已在故事板中启用)
- cellForRowAt 函数 - 请参见下面的代码
cell.backgroundImageView.layer.borderWidth = 1
cell.backgroundImageView.layer.masksToBounds = true
cell.backgroundImageView.layer.cornerRadius = cell.backgroundImageView.frame.width / 2
感谢 Whosebug 社区可以帮助我解决这个神秘问题并显示一个正常的圆圈。
编辑:添加约束
<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"/>
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 中试试这个 class
转到您的 uitableViewCell
class 并使用下面的代码,它适用于我,也适用于您。
override func awakeFromNib() {
super.awakeFromNib()
backGroundImageView.layer.cornerRadius = backGroundImageView.frame.width / 2
backGroundImageView.clipsToBounds = true
}