首页 文章

Headers 未显示自定义UIButton类

提问于
浏览
0

我创建了一个自定义UIButton类,它具有圆形框架 . 问题是虽然我设置了他们没有显示的故事板中每个按钮的title属性 . 我最终得到了圆形按钮,里面没有 Headers . 我正在尝试使用默认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
    }
}

4 回答

  • 0

    尝试将所有内容放在 init 方法中并检查 .

    import Foundation
    import UIKit
    
    class MyOwnButton: UIButton {
    
        override init(frame: CGRect){
            super.init(frame: frame)
            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
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    
    
    }
    
  • 0

    你有没有将你的UIButton类设置为故事板中的自定义类?

  • 2

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

    重写函数后添加 super.layoutSubviews() .

  • 0

    这是 Swift 4 的工作代码

    import UIKit
    
    class MyOwnButton: UIButton {
        override init(frame: CGRect){
           super.init(frame: frame)
    
            commonSetup()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
    
            commonSetup()
        }
    
        private func commonSetup() {
            self.clipsToBounds = true
            self.titleLabel?.font = UIFont.systemFont(ofSize: 33.0)
            self.titleLabel?.textColor = UIColor.white
            self.layer.cornerRadius = self.frame.size.width / 2.0
            self.layer.borderColor = UIColor.white.cgColor
            self.layer.borderWidth = 2.0
        }
    }
    

相关问题