iOS - 无法在viewDidAppear中更改子视图的属性

时间:2016-06-08 18:01:29

标签: ios swift uiview

我在viewDidLoad方法中添加了子视图。 并在viewDidAppear中更改其属性,但不会取得成功。 真的不知道为什么,为这个愚蠢的错误坚持了几个小时??!

 var iv = InitView()
 override func viewDidLoad() {
        super.viewDidLoad()

        iv.frame = view.frame
        iv.hidden = true
        self.navigationController!.view.addSubview(iv)
}

viewDidAppear

此视图的每个属性都无法更改或修改

iv.hidden = false〜>没有工作

iv.label.text = "Test"〜>更新UILabel文本无法正常工作

我不想将这些代码放在viewDidLoad中,因为它会更长时间地显示启动画面。

这是InitView的代码

class InitView:UIView {

    var indicator = Indicator()
    var label = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.backgroundColor = UIColor.whiteColor()

        indicator.frame = self.frame
        indicator.autoresizingMask = [.FlexibleWidth,.FlexibleHeight]
        self.addSubview(indicator)
        self.indicator.Show()

        label.translatesAutoresizingMaskIntoConstraints = false
        label.textAlignment = .Center
        label.text = "Loading ..."
        if #available(iOS 8.2, *) {
            label.font = UIFont.systemFontOfSize(17, weight: UIFontWeightLight)
        } else {
            // Fallback on earlier versions
            label.font = UIFont.systemFontOfSize(17)
        }
        self.addSubview(label)

        var ac = [NSLayoutConstraint]()
        ac += NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[l]-0-|", options: [], metrics: nil, views: ["l":label])
        ac += NSLayoutConstraint.constraintsWithVisualFormat("V:[i]-10-[l(30)]", options: [], metrics: nil, views: ["l":label,"i":indicator.container])
        self.addConstraints(ac)

        //self.autoresizingMask = [.FlexibleWidth,.FlexibleHeight]
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

0 个答案:

没有答案