gpt4 book ai didi

ios - UIButton 子类来设置按钮 Xcode/Swift 的高度

转载 作者:可可西里 更新时间:2023-11-01 01:27:59 26 4
gpt4 key购买 nike

我创建了一个 UIButton 子类来为一批按钮添加渐变层,我想知道是否有办法设置按钮的高度,因为它们的高度都相同。这是我的渐变层代码,所以我想知道它是否可行以及我需要将它放在代码中的什么位置:

public class GradientButton: UIButton {

override public func layoutSubviews() {
super.layoutSubviews()
createGradientBackground()

}
func createGradientBackground() {

let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.layer.bounds

let color1 = UIColor(red:0.05, green:0.29, blue:0.49, alpha:1.0).CGColor as CGColorRef
let color2 = UIColor(red:0.08, green:0.23, blue:0.39, alpha:1.0).CGColor as CGColorRef

gradientLayer.colors = [color1, color2]

gradientLayer.locations = [0.0, 1.0]

self.layer.insertSublayer(gradientLayer, atIndex: 0)
}
}

最佳答案

当然,只需添加:

init() {
self.frame.size.height = 50.0
//Do other stuff you may want
}

或者:

func createGradientBackground() {

let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.layer.bounds

let color1 = UIColor(red:0.05, green:0.29, blue:0.49, alpha:1.0).CGColor as CGColorRef
let color2 = UIColor(red:0.08, green:0.23, blue:0.39, alpha:1.0).CGColor as CGColorRef

gradientLayer.colors = [color1, color2]

gradientLayer.locations = [0.0, 1.0]

self.layer.insertSublayer(gradientLayer, atIndex: 0)

//set custom height wanted
self.frame.size.height = 50.0
}

您可以在自定义 init() 中或在您的 createGradientBackground() 中执行此操作。但是,您必须牢记在 Storyboard 中对按钮设置的限制。

关于ios - UIButton 子类来设置按钮 Xcode/Swift 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40511286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com