【问题标题】:How do I make an UIImage/-View with rounded corners CGRect (Swift)如何制作带有圆角 CGRect (Swift) 的 UIImage/-View
【发布时间】:2014-10-18 00:38:37
【问题描述】:

如何在 Swift iOS Playground 上制作带有圆角的 UIImageView?
里面需要填充颜色。

【问题讨论】:

标签: uiimageview swift rounded-corners swift-playground


【解决方案1】:
let imageView = UIImageView(frame: CGRectMake(0, 0, 100, 100))
imageView.backgroundColor = UIColor.redColor()
imageView.layer.cornerRadius = 8.0
imageView.clipsToBounds = true

结果:

【讨论】:

    【解决方案2】:

    对于 swift 中的圆形图像框架,它对我有用的是:

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

    以及添加阴影:

    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】:

      试试这个,它对我有用。

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

      【讨论】:

        【解决方案4】:

        我厌倦了为每个 UIView 编写 set radius 和 mask to bound。所以我对 UIView 做了以下扩展。应该适用于每个 UIView 子类,虽然我没有测试过。当然,可以针对您使用的特定视图缩小扩展范围。

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

        如果您不传递任何特定值,它将默认为视图的一半宽度。

        【讨论】:

          【解决方案5】:

          斯威夫特 3.0、4.0

          如果您想使用情节提要。 我应用了它并确保启用了“剪辑到边界”。

          【讨论】:

            【解决方案6】:

            如果您想选择对每个 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
                    }
                }
            }
            

            【讨论】:

            • 完美运行,谢谢
            • 你也可以在setter中添加self.clipsToBounds = true,不用担心在interface builder中设置
            【解决方案7】:

            Swift 5.0:

            我个人的偏好是为像这样的特定更改提供一个额外的 swift 文件。然后我要做的是创建一个类,例如“RoundCorner”是我想在这种情况下更改为 View 元素的元素的子类。然后我将覆盖各个设置。

            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
                }
            }
            

            之后,您只需选择要更改的元素,并将自定义类设置为我们之前创建的类。

            Look at the screenshot here

            【讨论】:

              【解决方案8】:

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

              【讨论】:

                【解决方案9】:

                在 Swift 4 中,您可以像这样相当容易地做到这一点:

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

                如果您使用相当长的矩形图像,例如用于应用程序中的特色内容部分或其他任何内容,请确保将 Content Mode 设置为 Scale To Fit,否则边角会以某种方式变圆但会被严重切割它不会是一个完美的圆角,而是一个圆形,然后在它夹住的地方锐利地切割。

                【讨论】:

                  猜你喜欢
                  • 2022-01-12
                  • 2012-02-14
                  • 2011-06-20
                  • 2011-01-28
                  • 2011-10-29
                  • 2011-03-23
                  • 1970-01-01
                  • 2011-12-04
                  相关资源
                  最近更新 更多