UIView以编程方式创建了另一个不在Swift中工作的UIView

时间:2016-02-09 21:12:44

标签: swift uiview uiscrollview interface-builder

我试图以编程方式在UIView的顶部创建一个UIView,但我不能让它在顶部。 Interface Builder中的所有元素都以编程方式创建此视图的顶部:

这是问题的截图:

enter image description here

绿色方块应位于此UIScrollView的顶部...

viewDidLoad中():

    var DynamicView=UIView(frame: CGRectMake(100, 150, 100, 100))
    DynamicView.backgroundColor=UIColor.greenColor()
    DynamicView.layer.cornerRadius=25
    DynamicView.layer.borderWidth=2
    globalView.addSubview(DynamicView)

    view.bringSubviewToFront(DynamicView)  

Interface Builder:

interface builder

2 个答案:

答案 0 :(得分:0)

尝试使用insertSubview:aboveSubview:添加您的子视图,而不是使用addSubview。您可以通过这种方式指定z顺序。

答案 1 :(得分:0)

应该是这样的:

self.view.bringSubviewToFront(DynamicView)

您需要在父视图上调用bringSubviewToFront。