添加子视图不起作用

时间:2015-11-13 07:37:42

标签: ios xcode swift uiview popup

我正在尝试制作一个popUp视图。

我有

let popOver = UIView()
popOver.hidden = true
        popOver.frame = CGRectMake(self.view.frame.width / 2 - 150, self.view.frame.height / 2 - 150, 250, 250)

        popOver.backgroundColor = UIColor.whiteColor()

        let lbl = UILabel()
        lbl.text = "blablabla"
        lbl.backgroundColor = UIColor.redColor()
        lbl.textColor = UIColor.yellowColor()
        popOver.addSubview(lbl)
        lbl.center = popOver.center
        popOver.bringSubviewToFront(lbl)


        self.view.addSubview(popOver)
        popOver.center = self.view.center
        popOver.layer.cornerRadius = 9

        UIView.animateWithDuration(2.0, animations: {
            self.popOver.hidden = false
        })

    }

一切都很好,除了我试图执行我的应用程序时现在显示标签。我只看到白框:

我的错误是什么?

2 个答案:

答案 0 :(得分:1)

标签位置错误

let lbl = UILabel()

lbl.frame = CGRectMake(0, 0, 25, 25)

lbl.text = "blablabla"
lbl.backgroundColor = UIColor.redColor()
lbl.textColor = UIColor.yellowColor()
popOver.addSubview(lbl)

//lbl.center = popOver.center
lbl.center = CGPointMake(popOver.frame.width / 2, popOver.frame.height/2)

答案 1 :(得分:1)

试试这个

let popOver = UIView()
    popOver.hidden = true
    popOver.frame = CGRectMake(self.view.frame.width / 2 - 150, self.view.frame.height / 2 - 150, 250, 250)

    popOver.backgroundColor = UIColor.whiteColor()
    popOver.center = self.view.center
    popOver.layer.cornerRadius = 9
    self.view.addSubview(popOver)

    let lbl = UILabel()
    lbl.text = "blablabla"
    lbl.backgroundColor = UIColor.redColor()
    lbl.textColor = UIColor.yellowColor()
    lbl.frame = CGRectMake((popOver.frame.size.width / 2)-50,  (popOver.frame.size.height / 2)-50, 100,100)
    //lbl.center = popOver.center
    popOver.addSubview(lbl)
    popOver.bringSubviewToFront(lbl)


    UIView.animateWithDuration(1.0, animations: {
        popOver.hidden = false
    })

输出

enter image description here