调用UIButton Extension中的layoutSubviews之后,UIButtons setAttributedTitle消失

时间:2018-12-29 18:30:58

标签: swift subclass viewdidload layoutsubviews

我所有的UIButton都有一个圆角半径问题,但最终可以通过遵循(Setting corner radius through viewDidAppear() or viewWillLayoutSubviews()? )上的解决方案来解决该问题,但是,现在我已经“丢失”了所有属性按钮标题。我已经与这个问题进行了两周的交涉,感觉我非常想将其全部解决。如果这不是一个措辞不好的问题,我深表歉意,因为我还是Swift的初学者。

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

在我的Swift文件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文件之一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文件中,instantiatedLandscapeButtonViewController,我对带有tag1到tag30的UIButton进行了类似的设置:

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

}




}

1 个答案:

答案 0 :(得分:1)

您目前的代码既危险又非法:

extension UIButton {
   override open func layoutSubviews() {

不!您无法在override中执行extension!结果可能无法预测。实现可靠的圆形按钮的方法是使用子类 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)
    }
}

结果:

enter image description here