您好,为什么百分比栏会自己移动?用户界面

问题描述 投票:0回答:0

所以,如果我在百分比文本字段中写下数字 1,2,3,.....,16,那么百分比栏会根据百分比数字完美显示。但是如果我退出应用程序,然后再次打开应用程序,任何数字下降到 16,百分比栏显示 17%。

导入 UIKit

GoalViewController 类:UIViewController、UITextFieldDelegate {

var titleLabel: UILabel!
var percentageLabel: UILabel!
var percentLabel: UILabel!
var percentageTextField: UITextField!
var progressView: UIProgressView!

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}
override func viewDidLoad() {
    super.viewDidLoad()
    
    titleLabel = UILabel()
    titleLabel.text = "Count"
    titleLabel.font = UIFont.boldSystemFont(ofSize: 40)
    titleLabel.textColor = .white
    titleLabel.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(titleLabel)
    
    NSLayoutConstraint.activate([
        titleLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0),
        titleLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20),
    ])
    percentageLabel = UILabel()
    percentageLabel.text = "Progress:"
    percentageLabel.font = UIFont.boldSystemFont(ofSize: 20)
    percentageLabel.textColor = .white
    percentageLabel.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(percentageLabel)
    
    NSLayoutConstraint.activate([
        percentageLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 40),
        percentageLabel.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20),
    ])
    percentLabel = UILabel()
    percentLabel.text = "%"
    percentLabel.font = UIFont.boldSystemFont(ofSize: 20)
    percentLabel.textColor = .white
    percentLabel.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(percentLabel)
    
    NSLayoutConstraint.activate([
        percentLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 40),
        percentLabel.leadingAnchor.constraint(equalTo: percentageLabel.trailingAnchor, constant: 60),
    ])
    percentageTextField = UITextField()
    percentageTextField.placeholder = "n"
    percentageTextField.font = UIFont.systemFont(ofSize: 18)
    percentageTextField.keyboardType = .numbersAndPunctuation
    percentageTextField.borderStyle = .roundedRect
    percentageTextField.translatesAutoresizingMaskIntoConstraints = false
    percentageTextField.delegate = self
    percentageTextField.textAlignment = .center
    percentageTextField.addTarget(self, action: #selector(updateProgressBar), for: .editingDidEnd)
    view.addSubview(percentageTextField)
    
    NSLayoutConstraint.activate([
        percentageTextField.centerYAnchor.constraint(equalTo: percentageLabel.centerYAnchor),
        percentageTextField.leadingAnchor.constraint(equalTo: percentageLabel.trailingAnchor, constant: 10),
        percentageTextField.widthAnchor.constraint(equalToConstant: 45),
    ])
    
    if self.traitCollection.userInterfaceStyle == .dark {
        view.backgroundColor = .black
        titleLabel.textColor = .white
        percentageLabel.textColor = .white
        percentLabel.textColor = .white
    } else {
        view.backgroundColor = .white
        titleLabel.textColor = .black
        percentageLabel.textColor = .black
        percentLabel.textColor = .black
    }
    
    let progressView = UIProgressView(progressViewStyle: .default)
    progressView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(progressView)
    progressView.layer.cornerRadius = 10
    progressView.tag = 100
    progressView.layer.masksToBounds = true
    progressView.layer.sublayers![1].cornerRadius = 10
    progressView.subviews[1].clipsToBounds = true
    progressView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMaxYCorner]
    
    NSLayoutConstraint.activate([
        progressView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 85),
        progressView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 20),
        progressView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -20),
        progressView.heightAnchor.constraint(equalToConstant: 30)
    ])
}

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)
    
    if self.traitCollection.userInterfaceStyle == .dark {
        view.backgroundColor = .black
        titleLabel.textColor = .white
        percentageLabel.textColor = .white
        percentLabel.textColor = .white
    } else {
        view.backgroundColor = .white
        titleLabel.textColor = .black
        percentageLabel.textColor = .black
        percentLabel.textColor = .black
    }
}
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    
    if let progressView = view.subviews.first(where: { $0 is UIProgressView }) as? UIProgressView {
        progressView.layer.cornerRadius = 10
        progressView.layer.masksToBounds = true
        progressView.setNeedsLayout()
        progressView.layoutIfNeeded()
    }
}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    updateProgressBar()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}
@objc func updateProgressBar() {
    guard let percentageText = percentageTextField.text?.trimmingCharacters(in: .whitespacesAndNewlines),
          let percentage = Float(percentageText) else {
        return
    }
    
    let clampedPercentage = min(max(percentage, 0), 100)
    
    if let progressView = view.viewWithTag(100) as? UIProgressView {
        progressView.setProgress(clampedPercentage / 100.0, animated: true)
    }
}

} 这是我的代码,请帮助我!

ios uikit percentage
© www.soinside.com 2019 - 2024. All rights reserved.