在UIImageView的Draw中设置Corner Radius

时间:2017-03-03 20:25:22

标签: swift uiimageview drawing draw cornerradius

我在UIImageView的draw方法中设置了一些属性。但是,这些似乎根本没有任何影响。我看到没有圆角,也没有遮挡效果。视图如下:

//
//  RoundImage.swift
//
//

import UIKit

class RoundImage: UIImageView {

    //------------------
    //MARK: - Setup and Initialization
    //------------------

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.initialize()
    }

    override func draw(_ rect: CGRect) {
        super.draw(rect)

        self.layer.cornerRadius = 30
        self.layer.masksToBounds = true
        self.clipsToBounds = true
    }

    //Setups content, styles, and defaults for the view
    private func initialize(){
        self.initStyle()
    }

    //Sets static content for the view
    private func staticContent() {

    }

    //Styles the view's colors, borders, etc at initialization
    private func initStyle(){


    }

    //Styles the view for variables that must be set at runtime
    private func runtimeStyle(){

        //TODO: These values cannot be changed in interface builder, but they should be able to be

    }

    //------------------
    //MARK: - Interface Builder Methods
    //------------------

    //Sets the view up for interface builder with runtime styling and temp display values
    override func prepareForInterfaceBuilder() {
        self.runtimeStyle()
        self.staticContent()
    }

    //------------------
    //MARK: - Lifecycle Methods
    //------------------

    //Sets the view up with runtime styling and values
    override func awakeFromNib() {
        super.awakeFromNib()

        self.runtimeStyle()
            self.staticContent()
        }


    }

1 个答案:

答案 0 :(得分:0)

当子类化时UIImageView根本不调用draw方法。虽然记录很少,但是不允许这样做。在这种情况下,建议继承UIView,然后自己在draw方法中的视图上绘制图像。

更多信息:

drawRect not being called in my subclass of UIImageView