【发布时间】:2017-02-14 04:41:57
【问题描述】:
我一直在使用下面显示的代码来圆化视图的选定角,但是现在在作为图层的可调整大小的视图上实现这一点有困难吗?每次调整视图大小时都不会更新。
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 来设置标签的内容(我需要获取 collectionView 中心单元格的 indexPath 以便我可以设置文本标签)
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 中使用相同的代码(不检查中心索引),但结果是一样的。
当我不使用任何圆角时,标签确实会正确调整大小。
【问题讨论】:
-
不检查链接的问题(我期待答案),你能解释一下确切的问题吗?从 Obj-C 到 Swift 的转换非常简单。你试过什么?你的问题比较模糊。
-
特别是在你提到的问题中,特别注意在
viewDidLayoutSubviews中做resize
标签: ios swift uiview uibezierpath cashapelayer