等待一个 addSubview 成功后再添加另一个 Subview
Wait for an addSubview to be successful before adding another Subview
我正在尝试使用自动布局向我的 ViewController 添加多个子视图。因此,很多这些视图都依赖于锚点或其他。
我的class低于
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 UI
ImageView 出现但没有别的。尽管您可以看到它意味着要添加子视图。
如果我返回导航然后再次返回,所有视图都是正确的!
我在 addSubview(dateTitleView) 行上放置了一个块点,代码在我看到 resFrame 图像出现之前到达了那一行。
所以,我怀疑这些观点并没有出现,因为他们试图锚定一些尚不存在的东西。
我原以为函数会按顺序处理。我的意思是 showTime() 在 showResFrame() 完成之前不会被调用。这不是正在发生的事情吗?
func showResults() {
showResFrame()
showTime()
showDate()
}
您需要在 viewDidLoad
/ viewWillAppear
而不是 viewDidAppear
中更早地添加它们
override func viewWillAppear() {
super.viewWillAppear()
showResults()
}
viewDidAppear
在 vc 的视图出现时被调用,因此您会注意到添加
我正在尝试使用自动布局向我的 ViewController 添加多个子视图。因此,很多这些视图都依赖于锚点或其他。
我的class低于
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 UI ImageView 出现但没有别的。尽管您可以看到它意味着要添加子视图。
如果我返回导航然后再次返回,所有视图都是正确的!
我在 addSubview(dateTitleView) 行上放置了一个块点,代码在我看到 resFrame 图像出现之前到达了那一行。
所以,我怀疑这些观点并没有出现,因为他们试图锚定一些尚不存在的东西。
我原以为函数会按顺序处理。我的意思是 showTime() 在 showResFrame() 完成之前不会被调用。这不是正在发生的事情吗?
func showResults() {
showResFrame()
showTime()
showDate()
}
您需要在 viewDidLoad
/ viewWillAppear
而不是 viewDidAppear
override func viewWillAppear() {
super.viewWillAppear()
showResults()
}
viewDidAppear
在 vc 的视图出现时被调用,因此您会注意到添加