Adding Label on Application Window from app delegate

谁说我不能喝 提交于 2019-12-10 09:32:26

问题


I am using Xcode 4.5 and iOS 6.0, I have selected the default single view template from the xcode, I just want to add the label on the application window but I am unable to do so. Please correct me.

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController"        bundle:nil];
self.window.rootViewController = self.viewController;
label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)];
label.text = @"Test";
[label setBackgroundColor:[UIColor whiteColor]];
[self.window addSubview:label];
[self.window bringSubviewToFront:label];
[self.window makeKeyAndVisible];
return YES;
}

PS - I want my label on top of my ViewController view i.e. on the window so it will be always there despite changes in the views presented by window. I dont want to only show the label here.

I got the answer

[self.window.rootViewController.view addSubview:label];

Thanks all for giving the pointers.


回答1:


Just Remove the RootviewController.

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)];
label.text = @"Test";
[label setBackgroundColor:[UIColor whiteColor]];
[self.window addSubview:label];
[self.window bringSubviewToFront:label];
[self.window makeKeyAndVisible];
return YES;
}

if you dont want to only show the label here then use like below.

self.viewController = [[ViewController alloc] initWithNibName:@"ViewController"        bundle:nil];
self.window.rootViewController = self.viewController;


[self.Viewcontroller.view addSubview:label];



回答2:


Add the label to self.window.rootViewController.view instead of self.window

UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)];
label.text = @"Test"; [label setBackgroundColor:[UIColor whiteColor]];
[self.window.rootViewController.view addSubview:label];


来源:https://stackoverflow.com/questions/13510835/adding-label-on-application-window-from-app-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!