将 ui 图像制作成 Swift 中的正圆
Making ui Image to perfect circle in Swift
我正在尝试通过 Swift 中的代码将 table 视图单元格中的 UIImage 视图制作成完美的圆形。
起初,我在我的代码中加入了 img.layer.cornerRadius = 35
,它使我的图像几乎像一个圆圈。然后我没有将数字传递给圆角半径,而是尝试通过代码编写该部分,例如 img.layer.cornerRadius = img.frame.height / 2
。但是,此代码使 UIImage 变为正方形(不应用圆角半径)。我不确定为什么会这样。任何人都可以解释为什么会发生这种情况以及如何解决它吗?
class RepositoryCell: UITableViewCell {
let userImageView: UIImageView = {
let img = UIImageView()
img.contentMode = .scaleAspectFit
img.layer.cornerRadius = img.frame.height / 2
// img.layer.cornerRadius = 35 <= this code works
img.clipsToBounds = true
img.backgroundColor = .black
return img
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(userImageView)
containerView.addSubview(userNameLabel)
containerView.addSubview(repositoryNameLabel)
addSubview(containerView)
addSubview(starNumLabel)
configureViewComponents()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configureViewComponents() {
setUserImageConstraints()
}
func setUserImageConstraints() {
userImageView.translatesAutoresizingMaskIntoConstraints = false
userImageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
userImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
userImageView.heightAnchor.constraint(equalToConstant: 80).isActive = true
userImageView.widthAnchor.constraint(equalTo: userImageView.heightAnchor, multiplier: 1).isActive = true
}
因为let img = UIImageView()
-> frame == zero
你可以这样做:
class RepositoryCell: UITableViewCell {
...
override func layoutSubviews() {
super.layoutSubviews()
userImageView.layer.cornerRadius = img.frame.height / 2
}
...
}
如果你使用 RxSwift,你可以简单地调用 img.cornerHalf()
,
它使用关键路径观察
let userImageView: UIImageView = {
let img = UIImageView()
// ...
img.cornerHalf()
// ...
return img
}()
使用下面的辅助代码
import RxSwift
import RxCocoa
extension UIView{
func cornerHalf(){
clipsToBounds = true
rx.observe(CGRect.self, #keyPath(UIView.bounds))
.subscribe(onNext: { _ in
// _ : CGRect? in
self.layer.cornerRadius = self.bounds.height * 0.5
}).disposed(by: rx.disposeBag)
}
}
我正在尝试通过 Swift 中的代码将 table 视图单元格中的 UIImage 视图制作成完美的圆形。
起初,我在我的代码中加入了 img.layer.cornerRadius = 35
,它使我的图像几乎像一个圆圈。然后我没有将数字传递给圆角半径,而是尝试通过代码编写该部分,例如 img.layer.cornerRadius = img.frame.height / 2
。但是,此代码使 UIImage 变为正方形(不应用圆角半径)。我不确定为什么会这样。任何人都可以解释为什么会发生这种情况以及如何解决它吗?
class RepositoryCell: UITableViewCell {
let userImageView: UIImageView = {
let img = UIImageView()
img.contentMode = .scaleAspectFit
img.layer.cornerRadius = img.frame.height / 2
// img.layer.cornerRadius = 35 <= this code works
img.clipsToBounds = true
img.backgroundColor = .black
return img
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(userImageView)
containerView.addSubview(userNameLabel)
containerView.addSubview(repositoryNameLabel)
addSubview(containerView)
addSubview(starNumLabel)
configureViewComponents()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configureViewComponents() {
setUserImageConstraints()
}
func setUserImageConstraints() {
userImageView.translatesAutoresizingMaskIntoConstraints = false
userImageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
userImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
userImageView.heightAnchor.constraint(equalToConstant: 80).isActive = true
userImageView.widthAnchor.constraint(equalTo: userImageView.heightAnchor, multiplier: 1).isActive = true
}
因为let img = UIImageView()
-> frame == zero
你可以这样做:
class RepositoryCell: UITableViewCell {
...
override func layoutSubviews() {
super.layoutSubviews()
userImageView.layer.cornerRadius = img.frame.height / 2
}
...
}
如果你使用 RxSwift,你可以简单地调用 img.cornerHalf()
,
它使用关键路径观察
let userImageView: UIImageView = {
let img = UIImageView()
// ...
img.cornerHalf()
// ...
return img
}()
使用下面的辅助代码
import RxSwift
import RxCocoa
extension UIView{
func cornerHalf(){
clipsToBounds = true
rx.observe(CGRect.self, #keyPath(UIView.bounds))
.subscribe(onNext: { _ in
// _ : CGRect? in
self.layer.cornerRadius = self.bounds.height * 0.5
}).disposed(by: rx.disposeBag)
}
}