无法添加标签以查看

时间:2019-09-16 22:02:35

标签: ios swift

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

我尝试在viewWillAppear中调用getContacts(),但仍然没有实现

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)     
    }
}

1 个答案:

答案 0 :(得分:1)

问题是这一行:

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

它会将标签移出您无法查看的联系人视图。