可调整大小的视图的圆角
我一直使用下面显示的代码将选定的视角四舍五入,但是现在在作为图层的可调整大小的视图上实现此操作时遇到了问题?每次视图调整大小时都不会更新。可调整大小的视图的圆角
extension UIView {
func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
titleLabelView.roundCorners(corners: [.topLeft, .topRight], radius: 10)
还有一个类似的问题here但它的相当老,都在objC完成。
是否有方法将swift中选定的角落舍入为可调整大小的视图?
----- -----编辑
所以基本上我有什么是我设置基于文本的大小来调整文本表。
在大多数情况下,我可以只使用:
myTextLabel.layer.cornerRadius = 10
但是这确实所有4个角落。所以,如果我想轮到顶部2,那么我需要使用上面的扩展名。现在,因为我使用scrollViewDidEndDecelerating来设置标签内容(我需要得到该小区的indexPath在的CollectionView的中心,这样我就可以设置文字标签)
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
GeneralFunctions.getIndexOfCell(sender: self) { (index) in
self.titleLabel.text = self.partArray[index].title
self.titleLabel.roundCorners(corners: [.topLeft, .topRight], radius: 10)
self.descriptionLabel.text = self.partArray[index].description
self.descriptionLabel.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 10)
self.backgroundLabelView.layer.cornerRadius = 16
self.backgroundLabelView.layoutIfNeeded()
self.backgroundLabelView.layoutSubviews()
}
}
使用viewDidLayoutSubViews不起作用在这种情况下,因为加速结束和布局之间存在滞后。我曾尝试在viewDidLayoutSubViews中使用相同的代码(没有检查中心索引),但结果相同。
和标签不正确调整的时候,不使用任何圆角。
我有类似的问题,我已经在使用此功能解决它像
DispatchQueue.main.async(execute: {
self.roundCorners(.allCorners, radius: 6, borderColor: nil, borderWidth: nil)
})
我使用这个功能很多关于UITableViewCell
,我的整个代码看起来像
private var didLayoutSubview = false {
didSet {
DispatchQueue.main.async(execute: {
self.roundCorners(.allCorners, radius: 6, borderColor: nil, borderWidth: nil)
})
}
}
override func layoutSubviews() {
if !self.didLayoutSubview {
self.didLayoutSubview = true
}
super.layoutSubviews()
}
所以基本上,在主线程中调用这个函数是有帮助的,我在layoutSubivews
里面调用它,因为我认为它是地方。
我的功能
func roundCorners(_ corners: UIRectCorner, radius: CGFloat, borderColor: UIColor?, borderWidth: CGFloat?) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.frame = self.bounds
mask.path = path.cgPath
self.layer.mask = mask
if borderWidth != nil {
addBorder(mask, borderWidth: borderWidth!, borderColor: borderColor!)
}
}
你有没有分类uiView来达到这个目的,还是你在喂它一个uiView? – DuckMan
我已经添加了我的函数,它是'UIView'的扩展,就像你的函数一样。我也设置'mask.frame'而不是将其设置为我的视图。 – JuicyFruit
而不检查链接的问题(我希望回答),你能解释一下确切的问题?从Obj-C到Swift的转换非常简单。你有什么尝试?你的问题相当模糊。 – dfd
特别是,在你引用的问题中,特别要注意在'viewDidLayoutSubviews'中调整大小。 –