Search code examples
iosswiftimagebackgroundibdesignable

How to cut corner of an image



I make this view with a swift file

import UIKit

@IBDesignable class DesignView: UIView {

@IBInspectable var CornerRadius: CGFloat = 0 {
    didSet {
        self.layer.cornerRadius = CornerRadius
    }
  }

}

But when i set a image background the image overflow of the view like this :

I tried to apply the same swift file to my image view like this

import UIKit

@IBDesignable class DesignImageView: UIImageView {

@IBInspectable var CornerRadius: CGFloat = 0 {
    didSet {
        self.layer.cornerRadius = CornerRadius
    }
  }

}

But nothing append so how can i cut the corner without edit my image ?


Solution

  • Try

    view.clipsToBound = true
    

    Your Image is showing out of its bounds because of its size

    From Apple Developer Docs:

    Setting this value to true causes subviews to be clipped to the bounds of the receiver. If set to false, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is false.