ios - 自定义 UIButton 类的标题未显示

标签 ios swift uibutton

我创建了一个具有圆形框架的自定义 UIButton 类。问题是虽然我从他们没有显示的 Storyboard 中设置了每个按钮的标题属性。我最终得到了圆形按钮,里面没有显示任何标题。我正在尝试做一个类似于默认 iOS 密码屏幕中的键盘。我的主视图背景颜色不是白色,我没有为按钮使用任何背景图像。这是自定义 UIButton 类的代码。

import Foundation
import UIKit

class MyOwnButton: UIButton {

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

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

    override func layoutSubviews() {
        self.clipsToBounds = true
        self.titleLabel?.font = UIFont.systemFontOfSize(33.0)
        self.titleLabel?.textColor = UIColor.whiteColor()
        self.layer.cornerRadius = self.frame.size.width / 2.0
        self.layer.borderColor = UIColor.whiteColor().CGColor
        self.layer.borderWidth = 2.0
    }
}

最佳答案

有同样的问题,这个答案对我有用:Custom UIButton subclass not displaying title

在覆盖函数后添加 super.layoutSubviews()

关于ios - 自定义 UIButton 类的标题未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38435937/

相关文章:

ios - 如何从 AppDelegate 更新某些 ViewController 的标签?

swift - 停止从不同的 View Controller 更新位置

ios - 如何更改启用 "glow"时使用的 UIButton "Shows Touch On Highlight"颜色?

ios - 检测哪个按钮需要更改单元格中的文本

Swift 4 - 为多个按钮创建通用功能

ios - 将 NavigationBar 背景设置为纯色

android - iOS开发中的getter/setter类

android - 绑定(bind) fbo 时是否需要每帧调用 glFramebufferTexture2D?

ios - 应用程序崩溃,即使除了带有 Swift 2 的 Xcode 中的代码之外什么都没有

ios - 在 cellForRowAt 中手动触发 didSelectRowAtIndexPath 会导致实际 didSelectRowAtIndexPath 委托(delegate)方法中的单元格为零