編輯:使用接受的答案中的配置方法解決了問題。使用 setTitle 設定標題會更簡單,但不可能,因為它必須是屬性字串。
在我目前正在處理的一個專案中,有一個 UITableView 從自定義類中獲取其單元格。每個單元格內都有一個標簽和一個按鈕。我在自定義類的 .xib 檔案中為這兩個元素的文本/字體設定默認值,然后在運行時覆寫這些值(這樣做的原因是縮放所有 UI 元素的大小和間距以適應任何尺寸的設備螢屏,當當前設備直到運行時才知道時)。
標簽按我的意圖作業,并保留以編程方式分配的新值。問題是按鈕沒有。它的文本開始顯示新值,但是一旦單擊它就會默認回傳到舊的 Storyboard 值,并且不會回傳到運行時值。我不希望顯示占位符值,只顯示我在 setLabelValues 函式中設定的值。
這是導致問題的代碼(一個最小的可重現示例,不是原始程式,但它表現出完全相同的問題):
自定義表格查看單元:
import UIKit
class testCellTableViewCell: UITableViewCell {
@IBOutlet weak var textLabelLeading: NSLayoutConstraint!
@IBOutlet weak var textLabelTrailing: NSLayoutConstraint!
@IBOutlet weak var buttonLabelLeading: NSLayoutConstraint!
@IBOutlet weak var buttonLabelTrailing: NSLayoutConstraint!
@IBOutlet weak var workingCorrectlyLabel: UILabel!
@IBOutlet weak var buggyButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
}
func setLabelValues(screenWidth: CGFloat, screenHeight: CGFloat) {
textLabelLeading.constant = 0.025 * screenWidth
textLabelTrailing.constant = -0.4 * screenWidth
buttonLabelLeading.constant = 0.6 * screenWidth
buttonLabelTrailing.constant = -0.025 * screenWidth
workingCorrectlyLabel.font = UIFont.systemFont(ofSize: 0.025*screenHeight, weight: .regular)
buggyButton.titleLabel?.text = "Fixed!"
buggyButton.titleLabel?.textColor = .green
buggyButton.titleLabel?.font = UIFont.systemFont(ofSize: 0.025*screenHeight, weight: .medium)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
視圖控制器:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var testTable: UITableView!
var labelMessages = ["This is a test", "Second Cell", "Another Label"]
override func viewDidLoad() {
super.viewDidLoad()
testTable.dataSource = self
testTable.register(UINib(nibName: "testCellTableViewCell", bundle: nil), forCellReuseIdentifier: "testCellID")
testTable.layoutMargins = UIEdgeInsets.zero
testTable.separatorInset = UIEdgeInsets.zero
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
tableView.layer.backgroundColor = CGColor(red: 0.000, green: 0.000, blue: 0.000, alpha: 1.000)
return labelMessages.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = testTable.dequeueReusableCell(withIdentifier: "testCellID", for: indexPath) as! testCellTableViewCell
cell.workingCorrectlyLabel.text = labelMessages[indexPath.row]
cell.layoutMargins = UIEdgeInsets.zero
cell.setLabelValues(screenWidth: view.safeAreaLayoutGuide.layoutFrame.width, screenHeight: view.safeAreaLayoutGuide.layoutFrame.height)
return cell
}
}
這是 .xib 檔案故事板的圖片,顯示了默認值。顯示 xib 檔案和屬性檢查器的 Xcode 視窗圖片。按鈕的默認文本是系統紅色的字串“Buggy”。

uj5u.com熱心網友回復:
設定按鈕值的另一種方法是使用其配置。它通過使用它的狀態,而不是直接訪問 titleLabel,使您可以更好地控制按鈕屬性
例子:
func setLabelValues(screenWidth: CGFloat, screenHeight: CGFloat) {
textLabelLeading.constant = 0.025 * screenWidth
textLabelTrailing.constant = -0.4 * screenWidth
buttonLabelLeading.constant = 0.6 * screenWidth
buttonLabelTrailing.constant = -0.025 * screenWidth
workingCorrectlyLabel.font = UIFont.systemFont(ofSize: 0.025*screenHeight, weight: .regular)
// Original way
buggyButton.titleLabel?.text = "Fixed!"
buggyButton.titleLabel?.textColor = .green
buggyButton.titleLabel?.font = UIFont.systemFont(ofSize: 0.025*screenHeight, weight: .medium)
// Configuration way
// this is for the attributed title to set on the button later
var container = AttributeContainer()
container.font = UIFont.systemFont(ofSize: 0.025*screenHeight, weight: .medium)
buggyButton.configurationUpdateHandler = { button in
switch button.state {
// you can switch over the different button states here, but i'll use default to make all button states the same
default:
button.configuration = .plain() // or .filled() or .tinted() whatever you prefer
button.configuration?.attributedTitle = AttributedString("Fixed", attributes: container)
button.configuration?.baseForegroundColor = .green
}
您也可以使用按鈕的 setTitle 方法設定按鈕的標題:
buggyButton.setTitle("Fixed", for: .normal)
看看這種方式是否會給您帶來任何問題...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/437381.html
下一篇:Swift顯示價格取決于區域設定
