【问题标题】:Custom UIButton background color not working自定义 UIButton 背景颜色不起作用
【发布时间】:2021-03-17 13:17:03
【问题描述】:

我创建了一个自定义 UIButton 以在我的应用程序中以编程方式使用。在一个屏幕上它工作正常。另一方面,背景没有出现。我查找了许多类似的问题,并将代码与它在工作时使用的其他 View Controller 进行了比较,并且没有明显的原因。为什么背景颜色不显示?

自定义按钮类

import Foundation
import UIKit

class PillButton: UIButton {

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

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

private func initializeButton() {
    backgroundColor = UIColor.white
    setTitleColor(UIColor(named: "pink"), for: .normal)
    contentEdgeInsets = UIEdgeInsets.init(top: 16, left: 48, bottom: 16, right: 48)
    translatesAutoresizingMaskIntoConstraints = false
}

override func layoutSubviews() {
    super.layoutSubviews()
    let height = frame.height / 2
    layer.cornerRadius = height
}
}

视图控制器

import Foundation
import UIKit
import MaterialComponents

class EventViewController: BaseViewController {

    private static let HORIZONTAL_PADDING: CGFloat = 16

    private var confirmButton: PillButton!
    private var unableToAttendButton: UILabel!
    private var signedUpLabel: UILabel!
    private var baseScrollView: UIScrollView!
    var event: Event!
    private var viewModel: EventViewModel = EventViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        createView()
    }

    override func createView() {
        super.createView()
        createConfirmButton()
    }


    private func createConfirmButton() {
        confirmButton = PillButton()

        let descriptionBottomGuide = UILayoutGuide()

        baseScrollView.addSubview(confirmButton)
        baseScrollView.addLayoutGuide(descriptionBottomGuide)

        descriptionBottomGuide.topAnchor.constraint(equalTo: eventDescription.bottomAnchor).isActive = true

        confirmButton.centerXAnchor.constraint(equalTo: baseScrollView.centerXAnchor).isActive = true
        confirmButton.topAnchor.constraint(equalTo: descriptionBottomGuide.bottomAnchor, constant: 20).isActive = true
    }

}

【问题讨论】:

  • 按钮代码看起来不错,但 ViewController 的逻辑不能完全重现 - 可能您应该提供创建 baseScrollView 的代码和生成的布局的屏幕截图(按钮颜色错误)

标签: ios swift uibutton


【解决方案1】:

您发布的代码包含很多您未提供的信息,因此很难知道发生了什么。

也就是说,您的 PillButton 课程存在一些问题:

  • 您应该layoutSubviews() 中调用initializeButton
  • 应该更新layoutSubviews()中的圆角半径
  • 无需覆盖setTitle
  • 无需设置layer背景色,并且您已经设置了按钮的背景色,无需再次设置。

另外,在您发布的代码中,您没有在任何地方设置按钮标题。

尝试用这个替换你的 PillButton 类,看看你是否能得到更好的结果:

class PillButton: UIButton {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeButton()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initializeButton()
    }
    
    private func initializeButton() {
        backgroundColor = Colors.black
        setTitleColor(UIColor(named: "pink"), for: .normal)
        contentEdgeInsets = UIEdgeInsets.init(top: 16, left: 48, bottom: 16, right: 48)
        translatesAutoresizingMaskIntoConstraints = false
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // update corner radius here!
        layer.cornerRadius = bounds.height / 2
    }
    
}

如果您不这样做,那么您需要通过其余代码(您没有在此处发布)进行一些调试,以了解发生了什么。

【讨论】:

  • 我已经发布了所有与 PillButton 有任何交互的代码
  • 编辑错误,我错过了一个地方。我将另一个视图的 topAnchor 设置为与 confirmButton 的底部锚点相等的那个点。这会破坏布局并导致背景颜色不显示。
【解决方案2】:
confirmButton = PillButton()

我会研究这段代码。自定义按钮类中指定的初始化程序,即带有框架和编码器的初始化程序调用 initializeButton(),但您没有实现 init() 来做同样的事情。

我会把它改成confirmButton = PillButton(frame:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 2023-03-14
    • 2018-06-29
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多