如何在场景码头中引用UIView?

时间:2015-07-11 08:36:45

标签: uiview uiviewcontroller uistoryboard

我在Dock中有UIViewController个2 UIViews个(场景): enter image description here

如何在我UIViewController中需要的任何地方展示它?换句话说,如何从我的控制器中引用这些视图?

2 个答案:

答案 0 :(得分:1)

在我的UIViewController代码中,使用@IBOutlet保留引用:

@IBOutlet weak var dockView: UIView!

答案 1 :(得分:0)

Swift 3,Xcode 8

1。创建参考

您可以将UIViews拖动到ViewController并为它们创建出口。 enter image description here

2。显示UIView

您想将其作为子视图添加到视图中。我连接了你在我的场景中看到的那两个按钮:

class ViewController: UIViewController {

    @IBOutlet var view1: UIView!
    @IBOutlet var view2: UIView!

    // SHOW THE VIEWS
    @IBAction func showView1(_ sender: UIButton) {
        view.addSubview(view1)
        view1.center = view.center
    }

    @IBAction func showView2(_ sender: UIButton) {
        view.addSubview(view2)
        view2.center = CGPoint(x: view.center.x, y: 100)
    }
}

3。隐藏UIViews

您必须从视图中删除它们。我为UIViews上的关闭按钮创建了动作:

class ViewController: UIViewController {

    @IBOutlet var view1: UIView!
    @IBOutlet var view2: UIView!

    // SHOW THE VIEWS
    @IBAction func showView1(_ sender: UIButton) {
        view.addSubview(view1)
        view1.center = view.center
    }

    @IBAction func showView2(_ sender: UIButton) {
        view.addSubview(view2)
        view2.center = CGPoint(x: view.center.x, y: 100)
    }

    // HIDE THE VIEWS
    @IBAction func hideView1(_ sender: UIButton) {
        view1.removeFromSuperview()
    }

    @IBAction func hideView2(_ sender: UIButton) {
        view2.removeFromSuperview()
    }
}

希望这有帮助!