【问题标题】:ios14 - Button Title and Image Not getting Displayedios14 - 按钮标题和图像未显示
【发布时间】:2021-01-24 08:43:52
【问题描述】:

按钮标题和图像未呈现。在iOS13及以下可以正常使用,但是更新到Xcode12.0.1和iOS14之后就不行了。

let viewAllButton: RoundedButton = {
   let button = RoundedButton()
    button.translatesAutoresizingMaskIntoConstraints = false
    button.backgroundColor = .red
    button.setTitle("View all", for: .normal)
    button.setTitleColor(.white, for: .normal)
    return button
}()

 class RoundedButton: UIButton {
     override func draw(_ rect: CGRect) {
       super.draw(rect)
       self.layer.cornerRadius = self.bounds.height / 2
       self.layer.masksToBounds = true
     }
}

【问题讨论】:

  • 有人遇到同样的问题吗?
  • 你能分享你的代码吗?
  • 是的.......
  • 请展示真实代码,包括 RoundedButton 是什么。见minimal reproducible example
  • 完成了!!...@matt

标签: ios swift uibutton uikit ios14


【解决方案1】:

Xcode 12.0.1、iOS 14、Swift 5.3

实际上问题出在我的代码库中的冗余代码上。

extension UIButton {
    open override func layoutSubviews() {
        super.layoutSubviews()
      }
  }

我刚刚把它放在我的扩展中。这在 iOS 13 及更低版本中没有导致任何问题。

重现步骤:

  1. 在您的项目中添加上述代码。
  2. 在您的模拟器中运行项目(发布或调试模式)项目(按钮正确呈现)并终止应用。
  3. 现在,通过单击应用图标启动应用,您将看到 Button 内容消失。

奇怪的错误,我希望它能节省别人的时间。

【讨论】:

    【解决方案2】:

    在设置 setTitle 之前尝试设置标题颜色。 (我也遇到过类似的问题)。

    【讨论】:

    • 它发生在整个应用程序的所有按钮上,以编程方式或通过情节提要
    猜你喜欢
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2014-05-07
    相关资源
    最近更新 更多