Ashwinkumar Mangrulkar
Ashwinkumar Mangrulkar

Reputation: 2965

How to add UIView on window in app delegate in swift?

view object is not getting in addsubview bracket

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
    }

Upvotes: 0

Views: 12149

Answers (3)

Naresh Reddy M
Naresh Reddy M

Reputation: 1096

In addition to your code, for UIApplication Root view controller's viewDidAppear Function add below code.

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

Hope that helps :)

Upvotes: 2

Bruno
Bruno

Reputation: 1592

Try to use so if access through UIViewController

let win:UIWindow = UIApplication.sharedApplication().delegate!.window!!
win.addSubview(self)

Upvotes: 9

holex
holex

Reputation: 24041

please, do the implementation for the stack properly, like e.g. this:

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
}

Upvotes: 4

Related Questions