view.Layer.CornerRadius没有使用UIVcrollView子视图的UIView Swift 3 iOS

时间:2016-12-13 12:11:47

标签: ios uiview uiscrollview swift3 rounded-corners

我希望视图有圆角,所以我添加了

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

    }

}

我该怎么做才能修复它?

2 个答案:

答案 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