我正在尝试在UIView中添加标签。 UIView已通过故事板添加到viewcontroller中。
每当我尝试添加标签(使用文本框)时,它会将其添加到视图控制器而不是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"在这个截图中绿色标签的位置:
答案 0 :(得分:2)
问题出在这一行
label.center = CGPoint(x: 160, y: 284)
将其评为超出帧边界