结果页面上的空白屏幕:它去了哪里?
Posted
技术标签:
【中文标题】结果页面上的空白屏幕:它去了哪里?【英文标题】:blank screen on results page: where did it go? 【发布时间】:2018-05-03 16:34:18 【问题描述】:在这个阶段,应用程序应该显示测验的结果。因为我希望名称显示在与描述不同的行上/以不同的字体显示,所以我将其分成两个标签。目前,这两个标签都没有出现。我认为这个问题来自 setUpViews 和/或约束,但我不确定。
import UIKit
class ResultsController: UIViewController
let resultsLabel: UILabel =
let rlbl = UILabel()
rlbl.text = "Congratulations!"
rlbl.contentMode = .scaleToFill
rlbl.numberOfLines = 0
rlbl.translatesAutoresizingMaskIntoConstraints = false
rlbl.textAlignment = .center
rlbl.font = UIFont(name: "USIS 1949", size: 50)
rlbl.adjustsFontSizeToFitWidth = true
rlbl.numberOfLines=0
rlbl.sizeToFit()
rlbl.minimumScaleFactor = 0.1
return rlbl
()
let descriptionLabel: UILabel =
let dlbl = UILabel()
dlbl.text = "Congratulations!"
dlbl.contentMode = .scaleToFill
dlbl.numberOfLines = 0
dlbl.translatesAutoresizingMaskIntoConstraints = false
dlbl.textAlignment = .justified
dlbl.font = UIFont(name: "Flama", size: 30)
return dlbl
()
func setupViews()
self.view.addSubview(resultsLabel)
resultsLabel.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 150).isActive=true
resultsLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
resultsLabel.widthAnchor.constraint(equalToConstant: 500).isActive=true
self.view.addSubview(descriptionLabel)
descriptionLabel.topAnchor.constraint(equalTo: resultsLabel.bottomAnchor, constant: 20).isActive=true
descriptionLabel.heightAnchor.constraint(equalToConstant: 50).isActive=true
descriptionLabel.widthAnchor.constraint(equalToConstant: 150).isActive=true
descriptionLabel.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
descriptionLabel.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 0).isActive=true
override func viewDidLoad()
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(done(sender:)))
navigationItem.title = "Results"
view.backgroundColor = UIColor.white
let names = ["Ross", "Joey", "Chandler", "Monica", "Rachel", "Phoebe"]
let additional = ["Ross", "Joey", "Chandler", "Monica", "Rachel", "Phoebe"]
var score = 0
for question in QuestionController.questionsList
score += question.selectedAnswerIndex!
let result = names[score % names.count]
resultsLabel.text = "\(result)"
let description = additional[score % additional.count]
descriptionLabel.text = "\(description)"
@objc func done(sender: UIBarButtonItem)
navigationController?.popToRootViewController(animated: true)
class QuestionHeader: UITableViewHeaderFooterView
override init(reuseIdentifier: String?)
super.init(reuseIdentifier: reuseIdentifier)
setupViews()
let nameLabel: UILabel =
let label = UILabel()
label.text = "Sample Question"
label.font = UIFont.boldSystemFont(ofSize: 30)
label.translatesAutoresizingMaskIntoConstraints = false
return label
()
func setupViews()
addSubview(nameLabel)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
class AnswerCell: UITableViewCell
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
required init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
let nameLabel: UILabel =
let label = UILabel()
label.text = "Sample Answer"
label.font = UIFont.systemFont(ofSize: 30)
label.translatesAutoresizingMaskIntoConstraints = false
return label
()
func setupViews()
addSubview(nameLabel)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-16-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
【问题讨论】:
【参考方案1】:如果指的是ResultsController
,您不是在viewDidLoad()
中调用setUpViews()
。
【讨论】:
那么我只需要移动 setUpViews() 吗? 在您提供的代码中,您实现了 setUpViews 方法,但从不使用它。在方法 viewDidLoad 中的 super.viewDidLoad() 之后添加 setUpViews() 行以上是关于结果页面上的空白屏幕:它去了哪里?的主要内容,如果未能解决你的问题,请参考以下文章