如何在swift中的app delegate中添加UIView?

时间:2014-06-11 13:07:14

标签: swift

视图对象未进入addsubview括号

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
    {
            // Override point for customization after application launch.
            UIApplication.sharedApplication().setStatusBarHidden(true, animated: true);
            var myView = UIView(frame:CGRectMake(0, 200, 320, 100));
            myView.backgroundColor = UIColor.redColor()
            self.window.?.addSubview(myView)


            return true
    }

3 个答案:

答案 0 :(得分:9)

如果通过UIViewController访问

,请尝试使用
let win:UIWindow = UIApplication.sharedApplication().delegate!.window!!
win.addSubview(self)

答案 1 :(得分:4)

请正确执行堆栈的实现,例如这样:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {

    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let mainViewController: MainViewController = MainViewController(nibName: "MainViewController", bundle: nil)
    window!.rootViewController = mainViewController
    window!.makeKeyAndVisible()

    return true
}

答案 2 :(得分:2)

除了你的代码, for UIApplication Root视图控制器的viewDidAppear函数 添加以下代码。

override func viewDidAppear(animated: Bool)
{
    super.viewDidAppear(animated)
    UIApplication.sharedApplication().delegate!.window!!.bringSubviewToFront((UIApplication.sharedApplication().delegate!.window!!.subviews[0]))
}

希望有所帮助:)