提问者:小点点

未显示CornerRadius


我把拐角半径设为10。但图像视图不显示角半径。

    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.image = UIImage(named: "sample_image")
        imageView.layer.cornerRadius = 20/2
        imageView.contentMode = .scaleToFill
        imageView.clipsToBounds = true
        return imageView

    }()

我也尝试过使用imageview.layer.maskstoBounds=true,但也不起作用。

当我在图像视图上使用背景色时,如下所示,它完美地显示了带有圆角的视图。

    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.backgroundColor = UIColor.init(hexString: "#000000")
        imageView.layer.cornerRadius = 20/2
        imageView.contentMode = .scaleToFill
        imageView.clipsToBounds = true
        return imageView

    }()

我还尝试更改图像的图像、宽度和高度,以检查图像对于视图是否太大。但它也不起作用。

我就是这样调用ImageView的。

       view.addSubview(imageView)

        self.imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

        self.imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: imageViewTopMargin).isActive = true

        self.imageView.widthAnchor.constraint(equalToConstant: imageViewWidth).isActive = true

        self.imageView.heightAnchor.constraint(equalToConstant: imageViewHeight).isActive = true

共1个答案

匿名用户

imageView.layer.masksToBounds = true