我正在尝试使用自动布局将多个子视图添加到ViewController中。因此,这些视图中有很多依赖于锚点或其他。
我的课在下面
class historyView: UIViewController {
var resFrame : UIImageView = {
let image = UIImage(named: "historyFrame.png")
let imageView = UIImageView(image: image)
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
var timeTitleView : UITextView = {
let textView = UITextView()
let titleTxt = NSLocalizedString("Time", comment: "")
textView.text = titleTxt
textView.font = UIFont(name: "BPG Nino Mtavruli", size: 20)
textView.adjustsFontForContentSizeCategory = true
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()
var timeResView : UITextView = {
let textView = UITextView()
textView.font = UIFont(name: "BPG Nino Mtavruli", size: 20)
textView.adjustsFontForContentSizeCategory = true
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()
var dateTitleView : UITextView = {
let textView = UITextView()
let titleTxt = NSLocalizedString("Date", comment: "")
textView.text = titleTxt
textView.font = UIFont(name: "BPG Nino Mtavruli", size: 20)
textView.adjustsFontForContentSizeCategory = true
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()
var dateResView : UITextView = {
let textView = UITextView()
let comResClass = complileResults()
let resTxt = comResClass.getResult(key: "date")
textView.text = resTxt
textView.font = UIFont(name: "BPG Nino Mtavruli", size: 20)
textView.adjustsFontForContentSizeCategory = true
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()
override func viewDidAppear(_ animated: Bool) {
showResults()
}
func showResults() {
showResFrame()
showTime()
showDate()
}
func showResFrame() {
view.addSubview(resFrame)
resFrame.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
resFrame.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
resFrame.widthAnchor.constraint(equalToConstant: 350).isActive = true
resFrame.heightAnchor.constraint(equalToConstant: 500).isActive = true
showTime()
}
func showTime() {
showTimeTitle()
showTimeResult()
}
func showTimeTitle() {
let titleTxt = NSLocalizedString("Time", comment: "")
timeTitleView.text = titleTxt
timeTitleView.font = UIFont(name: "BPG Nino Mtavruli", size: 20)
timeTitleView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(timeTitleView)
timeTitleView.topAnchor.constraint(equalTo: resFrame.topAnchor, constant: 15).isActive = true
timeTitleView.leftAnchor.constraint(equalTo: resFrame.leftAnchor, constant: 175).isActive = true
timeTitleView.widthAnchor.constraint(equalToConstant: 80).isActive = true
timeTitleView.heightAnchor.constraint(equalToConstant: 30).isActive = true
}
func showTimeResult() {
let comResClass = complileResults()
let resTxt = comResClass.getResult(key: "time")
timeResView.text = resTxt
view.addSubview(timeResView)
timeResView.topAnchor.constraint(equalTo: resFrame.topAnchor, constant: 15).isActive = true
timeResView.leftAnchor.constraint(equalTo: resFrame.leftAnchor, constant: 255).isActive = true
timeResView.widthAnchor.constraint(equalToConstant: 80).isActive = true
timeResView.heightAnchor.constraint(equalToConstant: 30).isActive = true
}
func showDate() {
showDateTitle()
showDateResult()
}
func showDateTitle() {
view.addSubview(dateTitleView)
dateTitleView.topAnchor.constraint(equalTo: resFrame.topAnchor, constant: 15).isActive = true
dateTitleView.leftAnchor.constraint(equalTo: resFrame.leftAnchor, constant: 15).isActive = true
dateTitleView.widthAnchor.constraint(equalToConstant: 80).isActive = true
dateTitleView.heightAnchor.constraint(equalToConstant: 30).isActive = true
}
func showDateResult() {
let comResClass = complileResults()
let resTxt = comResClass.getResult(key: "date")
dateResView.text = resTxt
view.addSubview(dateResView)
dateResView.topAnchor.constraint(equalTo: resFrame.topAnchor, constant: 15).isActive = true
dateResView.leftAnchor.constraint(equalTo: resFrame.leftAnchor, constant: 95).isActive = true
dateResView.widthAnchor.constraint(equalToConstant: 80).isActive = true
dateResView.heightAnchor.constraint(equalToConstant: 30).isActive = true
}
}
当视图第一次出现时, resFrame 用户界面 ImageView出现,但仅此而已。即使您可以看到它也意味着要添加子视图。
如果我在导航中返回,然后再次返回,则所有视图都是正确的!
我在addSubview(dateTitleView)行上放置了一个块点,然后在看到resFrame图像出现之前,代码已到达该行。
因此,我怀疑视图没有出现,因为它们试图锚定尚不存在的视图。
我曾期望这些功能能够按顺序处理。我的意思是直到showResFrame()完成才调用showTime()。那不是正在发生的事情吗?
func showResults() {
showResFrame()
showTime()
showDate()
}
答案 0 :(得分:0)
您需要在viewDidLoad
/ viewWillAppear
而不是viewDidAppear
内早些添加它们
override func viewWillAppear() {
super.viewWillAppear()
showResults()
}
viewDidAppear
在显示vc的视图时被调用,因此您会注意到该添加