使用自动布局(snapkit)的循环视图?
Circular views with Autolayout (snapkit)?
我正在尝试制作一个圆形视图,它具有基于自动布局的自适应大小,目前我设置了约束,然后我尝试在 viewwilllayoutsubviews 方法中对图像进行圆形处理。
这导致视图形状奇特,不是圆形的,我该如何解决这个问题?
初始化:
profilePic = UIImageView(frame: CGRect.zero)
profilePic.clipsToBounds = true
profilePic.contentMode = .scaleAspectFill
约束:
profilePic.snp.makeConstraints { (make) -> Void in
make.centerX.equalTo(self).multipliedBy(0.80)
make.centerY.equalTo(self).multipliedBy(0.40)
make.size.equalTo(self).multipliedBy(0.22)
}
子视图:
override func viewWillLayoutSubviews() {
self.navigationMenuView.profilePic.layer.cornerRadius = self.navigationMenuView.profilePic.frame.size.width / 2.0
self.navigationMenuView.profilePic.layer.borderWidth = 2
self.navigationMenuView.profilePic.layer.borderColor = UIColor.white.cgColor
}
结果:
我猜你想要这个(对于简单的自动布局很抱歉,但我不使用 snapkit):
profilePic.heightAnchor.constraint(equalTo: profilePic.widthAnchor).isActive = true
profilePic.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.22).isActive = true
而不是这个:
make.size.equalTo(self).multipliedBy(0.22)
我的建议是不要把它当成一个从外面看的圆形视图。使视图本身符合圆形,以便您可以在任何地方使用它。
在视图内部为其提供约束,例如...
widthAnchor.constraint(equalTo: heightAnchor).isActive = true
这将使它变成正方形(尺寸未定)。
然后在函数中layoutSubviews
...
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = bounds.size.width * 0.5
}
这将使正方形变成圆形。
我遇到了同样的问题
这是我的解决方案:
let profilePicHeight: CGFloat = 30.0
将这行代码添加到您的约束中:
profilePic.snp.makeConstraints { (make) -> Void in
make.height.width.equalTo(self.profilePicHeight)
...
}
然后:
override func viewWillLayoutSubviews() {
self.navigationMenuView.profilePic.layer.cornerRadius = self.profilePicHeight / 2.0
...
}
我正在尝试制作一个圆形视图,它具有基于自动布局的自适应大小,目前我设置了约束,然后我尝试在 viewwilllayoutsubviews 方法中对图像进行圆形处理。
这导致视图形状奇特,不是圆形的,我该如何解决这个问题?
初始化:
profilePic = UIImageView(frame: CGRect.zero)
profilePic.clipsToBounds = true
profilePic.contentMode = .scaleAspectFill
约束:
profilePic.snp.makeConstraints { (make) -> Void in
make.centerX.equalTo(self).multipliedBy(0.80)
make.centerY.equalTo(self).multipliedBy(0.40)
make.size.equalTo(self).multipliedBy(0.22)
}
子视图:
override func viewWillLayoutSubviews() {
self.navigationMenuView.profilePic.layer.cornerRadius = self.navigationMenuView.profilePic.frame.size.width / 2.0
self.navigationMenuView.profilePic.layer.borderWidth = 2
self.navigationMenuView.profilePic.layer.borderColor = UIColor.white.cgColor
}
结果:
我猜你想要这个(对于简单的自动布局很抱歉,但我不使用 snapkit):
profilePic.heightAnchor.constraint(equalTo: profilePic.widthAnchor).isActive = true
profilePic.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.22).isActive = true
而不是这个:
make.size.equalTo(self).multipliedBy(0.22)
我的建议是不要把它当成一个从外面看的圆形视图。使视图本身符合圆形,以便您可以在任何地方使用它。
在视图内部为其提供约束,例如...
widthAnchor.constraint(equalTo: heightAnchor).isActive = true
这将使它变成正方形(尺寸未定)。
然后在函数中layoutSubviews
...
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = bounds.size.width * 0.5
}
这将使正方形变成圆形。
我遇到了同样的问题
这是我的解决方案:
let profilePicHeight: CGFloat = 30.0
将这行代码添加到您的约束中:
profilePic.snp.makeConstraints { (make) -> Void in
make.height.width.equalTo(self.profilePicHeight)
...
}
然后:
override func viewWillLayoutSubviews() {
self.navigationMenuView.profilePic.layer.cornerRadius = self.profilePicHeight / 2.0
...
}