首页 > 解决方案 > 无法添加标签以查看

问题描述

我正在尝试以contactsView编程方式向视图添加标签,但由于某种原因它没有被添加。这是我的代码,我没有收到任何错误,但什么也没得到。

我已经尝试调用 getContacts()viewWillAppear但仍然没有

class ModuleViewController: UIViewController {


    @IBOutlet weak var contactsView: UIView!


    override func viewDidLoad() {
        super.viewDidLoad()

        getContacts()
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }



    fileprivate func getContacts() {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
        label.center = CGPoint(x: 160, y: 285)
        label.textAlignment = .center
        label.textColor = .black
        label.text = "Test"
        self.contactsView.addSubview(label)     
    }
}

标签: iosswift

解决方案


问题是这一行:

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

它将标签移出您看不到的联系人视图。


推荐阅读