【问题标题】:How to cut corner of an image如何剪掉图像的角落
【发布时间】:2017-07-09 08:56:48
【问题描述】:


我使用 swift 文件制作此视图

import UIKit

@IBDesignable class DesignView: UIView {

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

}

但是当我设置图像背景时,视图的图像溢出如下:

我尝试像这样将相同的 swift 文件应用到我的图像视图

import UIKit

@IBDesignable class DesignImageView: UIImageView {

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

}

但没有附加任何内容,所以我如何在不编辑图像的情况下偷工减料?

【问题讨论】:

  • view.clipsToBound = true
  • @sasquatch 哦,它有效,谢谢^^ 我已经解决了这个问题两个小时,你在两秒钟内解决了它

标签: ios swift image background ibdesignable


【解决方案1】:

试试

view.clipsToBound = true

您的图片显示超出其范围,因为它的大小

来自Apple Developer Docs

将此值设置为 true 会导致子视图被裁剪到接收器的边界。如果设置为 false,则其帧超出接收器可见边界的子视图不会被剪裁。默认值为 false。

【讨论】:

    【解决方案2】:

    从您的Class Scope 中添加此代码

    extension UIView // Almost any Kind of View inherits UIView Ex: UIImageView 
    {
        func setCorner(withRadius radius : CGFloat)
        {
            self.layer.cornerRadius = radius
            self.clipsToBounds = true //your view won't cross the bound
        }
    }
    

    并使用 like 。 .

    DesignImageView.setCorner(withRadius: 5)

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 2014-03-02
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 2020-03-17
      • 1970-01-01
      • 2018-12-29
      相关资源
      最近更新 更多