UIButtons setAttributedTitle 在 UIButton Extension 中调用 layoutSubviews 后消失

UIButtons setAttributedTitle Disappears After Calling layoutSubviews in UIButton Extension

我的所有 UIButton 都存在圆角半径问题,但最终能够按照(Setting corner radius through viewDidAppear() or viewWillLayoutSubviews()? ) 但是,现在我拥有了 "lost" 我所有的属性按钮标题。几个星期以来,我一直在与这个问题作斗争,感觉我已经非常接近解决这个问题了。如果这是一个措辞不佳的问题,我深表歉意,因为我对 Swift 还是比较陌生。

这是我正在处理的应用程序: my default iOS calculator project

在我的 Swift-file、UIButtonExtension.swift 中,我有以下内容:

import Foundation
import UIKit

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

        let radius = min(bounds.width, bounds.height) / 2
        layer.cornerRadius = radius

    }
}

在我的另一个 Swift-file 中,myCalculatorViewController.swift,我有:

import UIKit

class myCalculatorViewController: UIViewController {

@IBOutlet weak var tag1_Button: UIButton!
@IBOutlet weak var tag2_Button: UIButton!
@IBOutlet weak var tag3_Button: UIButton!
//     .....
@IBOutlet weak var tag19_Button: UIButton!


override func viewDidLoad() {
super.viewDidLoad()

// main/basic calc button view controller

tag1_Button.titleLabel?.textAlignment = .center
tag1_Button.contentHorizontalAlignment = .center
tag1_Button.titleLabel?.numberOfLines = 1
let str_tag1_Button = NSMutableAttributedString(string: "AC")
str_tag1_Button.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 10), range: NSMakeRange(0, 2))    
str_tag1_Button.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0), range: NSMakeRange(0, 2))
tag1_Button.setAttributedTitle(str_tag1_Button, for: .normal) 
tag1_Button.backgroundColor = UIColor(red: 40/255.0, green: 247/255.0, blue: 45/255.0, alpha: 1.0)

// and so forth with the rest of the tag buttons up to tag19_Button that sets title, "="

}
}

接下来,在另一个 Swift-file,实例化的 LandscapeButtonViewController 中,我对带有 tag1 到 tag30 的 UIButtons 进行了类似的设置:

import UIKit

class instantiatedLandscapeButtonViewController: UIViewController {


@IBOutlet weak var tag1_Button: UIButton!
@IBOutlet weak var tag2_Button: UIButton!
@IBOutlet weak var tag3_Button: UIButton!
//     .....
@IBOutlet weak var tag30_Button: UIButton!

override func viewDidLoad() {
super.viewDidLoad()

// additional landscape buttons, setup

tag1_Button.titleLabel?.textAlignment = .center
tag1_Button.contentHorizontalAlignment = .center
tag1_Button.titleLabel?.numberOfLines = 1
let str_tag1_Button = NSMutableAttributedString(string: "mc")
str_tag1_Button.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize: 10), range: NSMakeRange(0, 2))
str_tag1_Button.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor(red: 251/255.0, green: 251/255.0, blue: 251/255.0, alpha: 1.0), range: NSMakeRange(0, 2))
tag1_Button.setAttributedTitle(str_tag1_Button, for: .normal)

// and so forth

}




}

您目前的代码是危险且非法的:

extension UIButton {
   override open func layoutSubviews() {

没有!您不能在 extension 中执行 override!结果可能无法预测。实现可靠圆形按钮的方法是子类UIButton,其中override合法的,并使用那个子类。

class MyCircularButton: UIButton {
    override open func layoutSubviews() {
        super.layoutSubviews()
        let radius = min(bounds.width, bounds.height) / 2
        layer.cornerRadius = radius
    }
}

现在,只要您需要圆形按钮,就可以使用 MyCircularButton 而不是普通的 UIButton。我不知道这是否会解决您遇到的问题,但这肯定是必需的第一步。

我像这样测试了你剩下的代码,它运行良好:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let tag1_Button = MyCircularButton()
        tag1_Button.frame = CGRect(x: 100, y: 100, width: 40, height: 40)
        tag1_Button.titleLabel?.textAlignment = .center
        tag1_Button.contentHorizontalAlignment = .center
        tag1_Button.titleLabel?.numberOfLines = 1
        let str_tag1_Button = NSMutableAttributedString(string: "AC")
        str_tag1_Button.addAttribute(.font, value: UIFont.systemFont(ofSize: 10), range: NSMakeRange(0, 2))
        str_tag1_Button.addAttribute(.foregroundColor, value: UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0), range: NSMakeRange(0, 2))
        tag1_Button.setAttributedTitle(str_tag1_Button, for: .normal)
        tag1_Button.backgroundColor = UIColor(red: 40/255.0, green: 247/255.0, blue: 45/255.0, alpha: 1.0)
        self.view.addSubview(tag1_Button)
    }
}

结果: