简体   繁体   English

用于设置按钮高度的 UIButton 子类 Xcode/Swift

[英]UIButton subclass to set height of a button Xcode/Swift

I have created a UIButton subclass to add a gradient layer to a batch of buttons and am wondering if there is a way to set the height of the buttons there as well because they'll all be the same height.我创建了一个 UIButton 子类来为一批按钮添加一个渐变层,我想知道是否有办法在那里设置按钮的高度,因为它们都是相同的高度。 This is my code for the gradient layer so I'm wondering if it's possible and where in the code I need to put it:这是我的渐变层代码,所以我想知道它是否可能以及我需要将它放在代码中的哪个位置:

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

Sure, just add:当然,只需添加:

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

OR:或:

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
}

You can do this in an custom init(), or inside of your createGradientBackground().您可以在自定义 init() 中或在 createGradientBackground() 中执行此操作。 You do have to keep in mind of what constraints you are setting on your buttons in your storyboard however.但是,您必须牢记在故事板中的按钮上设置了哪些约束。

If your button has auto layout then you can update constraints runtime as follows:如果您的按钮具有自动布局,那么您可以按如下方式更新约束运行时:

import UIKit

class CustomButton: UIButton {

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

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

    func setupConstraints() {
        NSLayoutConstraint.activate([
            self.heightAnchor.constraint(equalToConstant: 50)
        ])
    }

    override class var requiresConstraintBasedLayout: Bool {
        return true
    }
}

Here you have to set requiresConstraintBasedLayout .在这里你必须设置requiresConstraintBasedLayout

Custom views should override this to return true if they cannot layout correctly using autoresizing.如果自定义视图无法使用自动调整大小正确布局,则应覆盖此值以返回 true。

From Apple Documentation来自苹果文档

Hope it helps you.希望对你有帮助。

The simplest way to do it, is to override var intrinsicContentSize: CGSize .最简单的方法是覆盖var intrinsicContentSize: CGSize

Example:示例:

open class AMPrimaryButton: UIButton {
    
    open override var intrinsicContentSize: CGSize {
        var size = super.intrinsicContentSize
        size.height = 50
        return size
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM