【发布时间】:2018-12-29 18:30:58
【问题描述】:
我的所有 UIButtons 都存在圆角半径问题,但最终通过遵循 (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 的 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
}
}
【问题讨论】:
-
您需要发布一些代码,因为您的问题没有足够详细地解释您的问题。
-
好的,我做到了。谢谢你告诉我。
-
但是现在还不清楚是什么问题。你展示了一些配置一些按钮的代码:然后会发生什么?
-
“丢失了我的属性标题”到底是什么意思?
-
我丢失了为 UIButtons 设置的 setAttributedTitles。我有所有按钮的工作角半径,但现在它们没有显示标题。他们是空白/丢失。我正在尝试研究如何对其进行子类化,因为我认为这将解决问题。
标签: swift subclass viewdidload layoutsubviews