简体   繁体   English

如何制作带有圆角的 UIImage/-View CGRect (Swift)

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

How do I make a UIImageView with rounded corners on a Swift iOS Playground?如何在 Swift iOS Playground 上制作带圆角的 UIImageView?
Inside it needs to be filled with a color.在里面它需要填充一种颜色。

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

Result:结果:

在此处输入图片说明

For rounded circle image frame in swift, what it worked for me was:对于 swift 中的圆形图像框架,它对我有用的是:

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;

Try this, it worked for me.试试这个,它对我有用。

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

I was tired of writing set radius and mask to bound for each UIView.我厌倦了为每个 UIView 编写设置半径和掩码来绑定 So I made the following extenstion for UIView.所以我为 UIView 做了以下扩展。 Should work for every UIView subclass, though I have not tested it.应该适用于每个 UIView 子类,尽管我还没有测试过。 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.如果您不传递任何特定值,它将默认为视图的半宽。

Swift 3.0, 4.0斯威夫特 3.0、4.0

If you want to use the storyboard.如果你想使用故事板。 I applied this and make sure that "Clip to bounds" is enable.我应用了这个并确保启用“剪辑到边界”。

在此处输入图片说明

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如果您想有一个选项来舍入每个UIImageView ,您可以将此代码复制到您的项目中,而不必忘记检查clip to bounds并将其值设置为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
        }
    }
}

截屏

Swift 5.0:斯威夫特 5.0:

My personal preference is to have an extra swift file for specific changes like this one.我个人的偏好是为像这样的特定更改提供一个额外的 swift 文件。 What I do is then create a class eg "RoundCorner" which is a subclass of the element I want to change in this case a View element.然后我要做的是创建一个类,例如“RoundCorner”,它是我想在这种情况下更改为 View 元素的元素的子类。 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看这里的截图

在身份检查器的用户定义的运行时属性部分设置layer.cornerRadius = 10甚至适用于可重复元素,如表格单元格。

In Swift 4 you can do it fairly easily like this:在 Swift 4 中,你可以很容易地做到这一点:

yourUIImage.layer.cornerRadius = 10 // Set it how you prefer
yourUIImage.layer.masksToBounds = true

And if you use a rather long rectangle image, for example for a featured content part in your app or whatever, make sure the Content Mode is set to Scale To Fit because otherwise the corners will be somehow rounded but will cut badly and it will not be a perfect round corner but rather a rounded, then sharp cut where it clips.如果您使用相当长的矩形图像,例如用于您应用程序中的特色内容部分或其他任何内容,请确保将Content Mode设置为“ Scale To Fit ,否则角会以某种方式变圆但会被严重切割并且不会是一个完美的圆角,而是一个圆角,然后在它夹住的地方进行锐利的切割。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM