gpt4 book ai didi

ios - 等待 addSubview 成功后再添加另一个 Subview

转载 作者:行者123 更新时间:2023-11-29 05:46:22 25 4
gpt4 key购买 nike

我正在尝试使用自动布局向我的 ViewController 添加多个 subview 。因此,许多这些 View 都依赖于 anchor 或其他。

我的类(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
}

}

当 View 第一次出现时,resFrame UIImageView 出现,但没有其他内容。即使您可以看到它意味着添加 subview 。

如果我返回导航然后再次返回,所有 View 都是正确的!

我在 addSubview(dateTitleView) 行上放置了一个 block 点,并且在看到 resFrame 图像出现之前代码就到达了该行。

所以,我怀疑这些 View 没有出现,因为它们试图锚定尚不存在的东西。

我原以为函数会按顺序处理。我的意思是在 showResFrame() 完成之前不会调用 showTime() 。这不是正在发生的事情吗?

func showResults() {
showResFrame()
showTime()
showDate()
}

最佳答案

您需要先将它们添加到 viewDidLoad/viewWillAppear 中,而不是 viewDidAppear

override func viewWillAppear() {
super.viewWillAppear()
showResults()
}

viewDidAppear 在 vc 的 View 出现时被调用,所以你会注意到添加

关于ios - 等待 addSubview 成功后再添加另一个 Subview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56121293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com