How do I make an UIImage/-View with rounded corners CGRect (Swift)

谁都会走 提交于 2019-11-27 20:01:27

问题


How do I make a UIImageView with rounded corners on a Swift iOS Playground?
Inside it needs to be filled with a color.


回答1:


let imageView = UIImageView(frame: CGRectMake(0, 0, 100, 100))
imageView.backgroundColor = UIColor.redColor()
imageView.layer.cornerRadius = 8.0
imageView.clipsToBounds = true

Result:




回答2:


For rounded circle image frame in swift, what it worked for me was:

    self.profileImageView.image =  UIImage(named:"profileUser")
    self.profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2
    self.profileImageView.clipsToBounds = true

And for adding a shadow:

    self.profileImageView.layer.masksToBounds = NO;
    self.profileImageView.layer.cornerRadius = 8;
    self.profileImageView.shadowOffset = CGSizeMake(5.0, 5.0);
    self.profileImageView.shadowRadius = 5;
    self.profileImageView.shadowOpacity = 0.5;



回答3:


Try this, it worked for me.

self.profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2
self.profileImageView.clipsToBounds = true



回答4:


I was tired of writing set radius and mask to bound for each UIView. So I made the following extenstion for UIView. Should work for every UIView subclass, though I have not tested it. The extension can be narrowed down for specific Views you use of course.

extension UIView {      
    func setRadius(radius: CGFloat? = nil) {
        self.layer.cornerRadius = radius ?? self.frame.width / 2;
        self.layer.masksToBounds = true;
    }
}

It will default to the view's half width if you don't pass it any specific value.




回答5:


Swift 3.0, 4.0

If you want to use the storyboard. I applied this and make sure that "Clip to bounds" is enable.




回答6:


If you want to have an option to round each UIImageView, you can copy this code into your project without forgetting to check clip to bounds and set its value to true

import UIKit

@IBDesignable
extension UIImageView
{
    private struct AssociatedKey
    {
        static var rounded = "UIImageView.rounded"
    }

    @IBInspectable var rounded: Bool
    {
        get
        {
            if let rounded = objc_getAssociatedObject(self, &AssociatedKey.rounded) as? Bool
            {
                return rounded
            }
            else
            {
                return false
            }
        }
        set
        {
            objc_setAssociatedObject(self, &AssociatedKey.rounded, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            layer.cornerRadius = CGFloat(newValue ? 1.0 : 0.0)*min(bounds.width, bounds.height)/2
        }
    }
}




回答7:


Setting layer.cornerRadius = 10 at User Defined Runtime Attributes section on the Identity inspector works even on repeatable elements like table cells.




回答8:


Swift 5.0:

My personal preference is to have an extra swift file for specific changes like this one. What I do is then create a class e.g. "RoundCorner" which is a subclass of the element I want to change in this case a View element. And then I am overriding the individual settings.

class RoundCorner: UIView {
override func draw(_ rect: CGRect) {
    self.layer.cornerRadius = 10 // change this number to get the corners you want
    self.layer.masksToBounds = true
    }
}

After that, you only have to select the element you want this changes on, and set the custom class to the class we created earlier.

Look at the screenshot here



来源:https://stackoverflow.com/questions/25476139/how-do-i-make-an-uiimage-view-with-rounded-corners-cgrect-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!