【问题标题】:Round Corners in custom UICollectionViewCell not working in Swift自定义 UICollectionViewCell 中的圆角在 Swift 中不起作用
【发布时间】:2020-12-23 10:31:09
【问题描述】:

我有自定义 UICollectionViewCell 并尝试为按钮圆角,但这不起作用。 我对 ViewController 有同样的问题,问题是我在 viewDidLoad 而不是 subviewsDidLoad 中进行舍入。

我不知道现在有什么问题。

分享我的代码。

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        initialsButton.layer.cornerRadius = 0.5 * initialsButton.frame.size.width
        initialsButton.clipsToBounds = true
        initialsButton.layer.masksToBounds = true
    }

-> 但我也尝试过不使用 .clipsToBounds 和 .masksToBounds。结果一样。

这是结果。 It is not a circle, it makes corner wrongly SEE THIS RESULT

【问题讨论】:

  • 要对任何视图应用圆角,您只需要提供cornerRadius 并使clipsToBounds = true。就是这样。
  • @DharaPatel i.stack.imgur.com/3cb2x.png 看到这个结果。我做了cornerRadius和clipsToBounds。我的代码在这里。我有这个结果的问题是什么?
  • 如果您想制作圆形按钮,请确保您的按钮具有相同的高度和宽度。
  • 是的,它的比例是1:1

标签: ios swift cornerradius


【解决方案1】:

我想你正在使用 IBOutlet 的 创建一个类似这样的类,将按钮添加到情节提要中的单元格,在那里编辑你想要的任何东西,更容易。您的代码不好,只需删除 size,frame.height / 2 就是您想要的。

@IBDesignable
class RoundedBtn: UIButton {

@IBInspectable var cornerRadius: Double {
    get {
        return Double(self.layer.cornerRadius)
    }set {
        self.layer.cornerRadius = CGFloat(newValue)
    }
}

@IBInspectable var borderWidth: Double {
    get {
        return Double(self.layer.borderWidth)
    }
    set {
        self.layer.borderWidth = CGFloat(newValue)
    }
}

@IBInspectable var borderColor: UIColor? {
    get {
        return UIColor(cgColor: self.layer.borderColor!)
    }
    set {
        self.layer.borderColor = newValue?.cgColor
    }
}

@IBInspectable var shadowColor: UIColor? {
    get {
        return UIColor(cgColor: self.layer.shadowColor!)
    }
    set {
        self.layer.shadowColor = newValue?.cgColor
    }
}

@IBInspectable var shadowOffSet: CGSize {
    get {
        return self.layer.shadowOffset
    }
    set {
        self.layer.shadowOffset = newValue
    }
}

@IBInspectable var shadowRadius: Double {
    get {
        return Double(self.layer.shadowRadius)
    }set {
        self.layer.shadowRadius = CGFloat(newValue)
    }
}

@IBInspectable var shadowOpacity: Float {
    get {
        return self.layer.shadowOpacity
    }
    set {
        self.layer.shadowOpacity = newValue
    }
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    commonInit()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
}

func commonInit() {
    self.titleLabel?.numberOfLines = 0
    self.titleLabel?.textAlignment = .center
    self.setContentHuggingPriority(UILayoutPriority.defaultLow + 1, for: .vertical)
    self.setContentHuggingPriority(UILayoutPriority.defaultLow + 1, for: .horizontal)
}

override var intrinsicContentSize: CGSize {
    let size = self.titleLabel!.intrinsicContentSize
    return CGSize(width: size.width + contentEdgeInsets.left + contentEdgeInsets.right, height: size.height + contentEdgeInsets.top + contentEdgeInsets.bottom)
}

override func layoutSubviews() {
    super.layoutSubviews()
    titleLabel?.preferredMaxLayoutWidth = self.titleLabel!.frame.size.width
}

}

【讨论】:

  • 感谢分享,但整个想法是我想要一个圆形按钮,所以我必须使用 width/2 而不能使用一些恒定的像素来完成。但我创建了 RoundButton 类,但它不起作用。
【解决方案2】:

问题是我在 awakeFromNib() 中做圆角,而不是你必须在 layoutSubviews() 中做它,它工作得很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    相关资源
    最近更新 更多