【问题标题】:UIButton subclass to set height of a button Xcode/SwiftUIButton 子类设置按钮 Xcode/Swift 的高度
【发布时间】:2017-03-23 12:26:06
【问题描述】:

我创建了一个 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)
    }
}

【问题讨论】:

    标签: ios swift xcode uibutton


    【解决方案1】:

    最简单的方法是覆盖var intrinsicContentSize: CGSize

    例子:

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

    【讨论】:

      【解决方案2】:

      如果您的按钮具有自动布局,那么您可以按如下方式更新约束运行时:

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

      这里你必须设置requiresConstraintBasedLayout

      如果自定义视图无法使用自动调整大小正确布局,则应覆盖此参数以返回 true。

      来自Apple Documentation

      希望对你有帮助。

      【讨论】:

        【解决方案3】:

        当然,只需添加:

        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() 中执行此操作。但是,您必须牢记在情节提要中的按钮上设置了哪些约束。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-07-01
          • 1970-01-01
          • 2021-03-14
          • 1970-01-01
          • 2015-06-24
          • 2018-04-03
          • 2015-07-30
          相关资源
          最近更新 更多