gpt4 book ai didi

ios - 在 UiView 中添加标签

转载 作者:行者123 更新时间:2023-11-28 12:09:27 29 4
gpt4 key购买 nike

我正在尝试在 UIView 中添加标签。 UIView 已通过 Storyboard添加到 View Controller 。

每当我尝试添加标签(使用文本框)时,它都会将其添加到 View Controller 中,而不是在 uiview 中。这是代码,我哪里错了?

protocol AddContentUIViewDelegate: class {
func meathodForAddingChip(name: String)
}

class AddContentUIView: UIView {

weak var delegate:AddContentUIViewDelegate?

func addLabelFunc(name: String) {
delegate?.meathodForAddingChip( name: name)
}

func addSV(label: UILabel){
self.addSubview(label)
}
}

class TheVC: UIViewController, AddContentUIViewDelegate {

@IBOutlet weak var searchTextField: SearchTextField!
@IBOutlet weak var contentUIView: AddContentUIView!

override func viewDidLoad() {
super.viewDidLoad()

contentUIView.delegate = self

//Test to add label
contentUIView.addLabelFunc(name: item.title)
}

func meathodForAddingChip(name: String) {

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
label.textColor = .black
label.center = CGPoint(x: 160, y: 284)
label.textAlignment = .center
label.text = name
contentUIView.addSV(label: label)
}
}

我正在尝试让“John appleseed”成为此屏幕截图中绿色标签的位置:

enter image description here

最佳答案

问题出在这一行

  label.center = CGPoint(x: 160, y: 284)

在超出框架边界时对其进行评论

关于ios - 在 UiView 中添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48819034/

29 4 0