将UIView从ViewController移动到Window

时间:2018-05-11 16:12:57

标签: ios swift uiview uiviewcontroller window

感谢您花时间阅读。基本上,我的UIView中有一个UIViewController。我希望用户能够按一个按钮,然后UIView从我的UIViewController移动到我的应用程序窗口,以便UIView将首先UIViewControllers class ViewController: UIViewController { var window = UIApplication.shared.keyWindow! var view = UIView() override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(view) } func tappedAction() { window.bringSubview(toFront: view) } } 1}}。我唯一能想到的就是

readseg

但那并没有奏效。有没有人对如何做到这一点有任何想法?

3 个答案:

答案 0 :(得分:0)

您必须在var view = UIView()

处设置视图框架

然后你应该添加到窗口window.addSubview(view)

答案 1 :(得分:0)

如果您的视图已添加到窗口中,则window.bringSubview(toFront: view)将起作用,否则不起作用。

如果您的视图已添加到窗口中,那么您可以使用bringSubview(toFront:)这样: 例如:

        let window = UIApplication.shared.keyWindow!
        let view1 = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
        window.addSubview(view1);
        view1.backgroundColor = UIColor.black
        let view2 = UIView(frame: CGRect(x: 50, y: 50, width: 100, height: 50))
        view2.backgroundColor = UIColor.white
        window.addSubview(view2)
        UIApplication.shared.keyWindow!.bringSubview(toFront: view1)

因此您需要在窗口中添加视图:

    window.addSubview(view)

答案 2 :(得分:0)

您不能只将UIWindow中的子视图带到UIView的前面。

你需要:

  1. UIViewController
  2. 中删除UIView
  3. UIWindow添加到主import UIKit class ViewController: UIViewController { var customView: UIView! // Load the main view of the UIViewController. override func loadView() { view = UIView() } override func viewDidLoad() { super.viewDidLoad() // Load the custom view that we will be transferring. self.customView = UIView(frame: .init(x: 100, y: 250, width: 250, height: 250)) self.customView.backgroundColor = .red view.addSubview(customView) // Transfer the view. Call this method in your trigger function. transfer(self.customView) } func transfer(_ view: UIView) { // Remove the view from the UIViewController. view.removeFromSuperview() // Add the view to the UIWindow. UIApplication.shared.windows.first!.addSubview(view) } }
  4. 我选择这样做:

    JFXListView