0

I am setting some properties in the draw method of my UIImageView. However, these do not seem to be taking any affect at all. I see no rounded corners and no masking taking affect. The view is below:

//
//  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()
        }


    }
steventnorris
  • 5,656
  • 23
  • 93
  • 174
  • 1
    Might be silly thing to ask, but if you're using this for a UIImageView in the storyboard, did you make sure to set the class? If you have done so already, add a breakpoint in the `draw` function and check to see if it breaks there. – Pratik Patel Mar 03 '17 at 20:36
  • @PratikPatel Yeah I set the class. Oddly enough though, it isn't hitting my breakpoint in my draw method at all. – steventnorris Mar 03 '17 at 21:59

1 Answers1

0

UIImageView when subclassed does not call the draw method at all. It is not allowed, although minimally documented. In this case, it is recommended to subclass UIView and then draw the image on the view in your draw method yourself.

For further information:

drawRect not being called in my subclass of UIImageView

Community
  • 1
  • 1
steventnorris
  • 5,656
  • 23
  • 93
  • 174