【问题标题】:UIImageView not reshaping properly [duplicate]UIImageView 没有正确重塑 [重复]
【发布时间】:2015-10-08 08:20:41
【问题描述】:

代码如下:

    override func awakeFromNib() {
    super.awakeFromNib()


    profilePic.frame = CGRectMake(10, 7, 40, 40)
    profilePic.layer.cornerRadius = profilePic.frame.width/2
    profilePic.layer.masksToBounds = true

    contentView.addSubview(profilePic)
}

我一直用这段代码把图片做成圆形,现在变成圆角矩形了,怎么解决?

[edit] 这个方法每次我需要时都对我有用,但是现在我在一个单独的类中为 UITableView 的单元格执行此操作,它不会为图像赋予圆形!

【问题讨论】:

  • 使用image.clipsToBounds = true 并尝试
  • 不起作用@EICaptain
  • 你是否将 maskToBounds 设置为 false 并使用 clipsToBounds

标签: swift ios9


【解决方案1】:

如果我在 viewDidLoad 中给出了这个逻辑,它就会起作用

    testingImageView.layer.cornerRadius = testingImageView.frame.width/2
    testingImageView.layer.masksToBounds = true
    testingImageView.clipsToBounds = true
    self.view.addSubview(testingImageView)

【讨论】:

  • 我知道,这种方法在整个应用程序中都适用于我,但现在,我不这样做,它没有。
【解决方案2】:

试试这个代码:

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

希望对你有帮助:)

【讨论】:

  • 我试过了,还是不行,
  • @dpstart 它在我的应用程序中工作..
  • 它在我的应用程序上一直有效,但这一次,我不知道为什么,它没有。
  • 确保在答案中添加描述。欢迎来到 Stack Overflow 推荐阅读How to Answer
猜你喜欢
  • 2020-03-27
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多