gpt4 book ai didi

ios - 以编程方式添加 UILabel 不可见

转载 作者:搜寻专家 更新时间:2023-11-01 06:27:53 24 4
gpt4 key购买 nike

我正在尝试在这里练习,我的 View 中有一个 SearchBar 并且我已经使用过

  func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) 

一旦用户开始编写,我就隐藏我的 View ,并以编程方式创建另一个

这是我的概念,一旦我开始写作,我的第二个观点就会出现: Gif

但是一旦我正确添加了第二个 View ,我想向它添加一个 UILabel。这是我的代码:

    self.word_of_the_day_view.isHidden = true // main view

self.popularView.frame = CGRect(x: self.searcy_bar_view.frame.minX, y: self.searcy_bar_view.frame.maxY + 15, width: self.searcy_bar_view.frame.width, height: self.searcy_bar_view.frame.height) // don't mind the height and width


self.popularView.backgroundColor = .white
self.popularView.layer.cornerRadius = self.searcy_bar_view.layer.cornerRadius
self.popularView.popIn() // just an animation
is_popular_added = true

self.view.addSubview(popularView)

let label_1 = UILabel()
label_1.frame = CGRect(x: self.popularView.frame.minX + 3, y: self.popularView.frame.minY, width: self.popularView.frame.width, height: 20)
label_1.font = UIFont(name: "avenirnext-regular", size: 13)
label_1.text = "Hello World!"
label_1.layer.borderColor = UIColor.red.cgColor
popularView.addSubview(label_1)

但是在运行时,甚至没有添加 UILabel。

enter image description here

非常感谢您阅读这个问题!

最佳答案

您需要了解 Frame 和 Bounds 之间的区别。 View 的框架是 View 在其父 View 中的位置,因此他的原点可以具有任何 CGPoint 值。边界只是 View 本身,因此它始终具有 (0,0) 的原点

我觉得问题是

label_1.frame = CGRect(x: self.popularView.frame.minX + 3, y: self.popularView.frame.minY, width: self.popularView.frame.width, height: 20)

努力改变

label_1.frame = CGRect(x: self.popularView.bounds.minX + 3, y: self.popularView.bounds.minY, width: self.popularView.frame.width, height: 20)

label_1.frame = CGRect(x: 3, y: 0, width: self.popularView.frame.width, height: 20)

关于ios - 以编程方式添加 UILabel 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51494435/

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