【问题标题】:UIView created programmatically at top of another UIView not working in SwiftUIView 在另一个 UIView 顶部以编程方式在 Swift 中不工作
【发布时间】:2016-02-09 21:12:43
【问题描述】:

我正在尝试以编程方式在 UIView 顶部创建 UIView,但我没有让它位于顶部。 Interface Builder 中的所有元素都位于以编程方式创建的视图的顶部:

这是问题的截图:

绿色方块应该在这个 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)  

界面构建器:

【问题讨论】:

    标签: swift uiview uiscrollview interface-builder


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      应该是这样的:

      self.view.bringSubviewToFront(DynamicView)
      

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

      【讨论】:

        猜你喜欢
        • 2017-12-20
        • 2017-03-17
        • 1970-01-01
        • 2016-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-17
        相关资源
        最近更新 更多