我希望视图有圆角,所以我添加了
cardView.layer.cornerRadius = 5
但是cardView的子视图,即UIScrollView
的sViewListing似乎并没有受到它的影响。
我只想要topRight&将cornerRadius
的topLeft UIScrollView
设置为5,因此我尝试使用UIBezierPath
来屏蔽它,但它似乎仍然无法正常工作。
以下是我的尝试:
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet var cardView: UIView!
@IBOutlet var sViewListing: UIScrollView!
@IBOutlet var bookTitleListing: UILabel!
@IBOutlet var ratingListing: UIImageView!
@IBOutlet var locationListing: UILabel!
@IBOutlet var priceListing: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
let path = UIBezierPath(roundedRect:sViewListing.bounds,
byRoundingCorners:[.topRight, .topLeft],
cornerRadii: CGSize(width: 5, height: 5))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
sViewListing.layer.mask = maskLayer
cardView.backgroundColor = UIColor.white
cardView.layer.cornerRadius = 5
cardView.clipsToBounds = true
cardView.layer.masksToBounds = false
cardView.layer.shadowColor = UIColor.black.withAlphaComponent(0.3).cgColor
cardView.layer.shadowOffset = CGSize(width: 0, height: 0)
cardView.layer.shadowOpacity = 0.8
}
}
我该怎么做才能修复它?
答案 0 :(得分:3)
在另一个UIView中进一步嵌套它命名为maskedCardView并添加了代码
maskCardView.layer.cornerRadius = 5
maskCardView.layer.masksToBounds = true
添加插座后
@IBOutlet var maskCardView: UIView!
这样它可以在屏蔽topLeft& amp;屏幕时保持阴影。顶部UIScrollView的右角。这是完整的参考代码。
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet var cardView: UIView!
@IBOutlet var sViewListing: UIScrollView!
@IBOutlet var bookTitleListing: UILabel!
@IBOutlet var ratingListing: UIImageView!
@IBOutlet var locationListing: UILabel!
@IBOutlet var priceListing: UILabel!
@IBOutlet var maskCardView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
maskCardView.layer.cornerRadius = 5
maskCardView.layer.masksToBounds = true
cardView.backgroundColor = UIColor.white
cardView.layer.cornerRadius = 5
cardView.clipsToBounds = true
cardView.layer.masksToBounds = false
cardView.layer.shadowColor = UIColor.black.withAlphaComponent(0.3).cgColor
cardView.layer.shadowOffset = CGSize(width: 0, height: 0)
cardView.layer.shadowOpacity = 0.8
}
}
谢谢! :)
答案 1 :(得分:0)
成功:
cardView.layer.masksToBounds = true
sViewListing.layer.masksToBounds = true