开发者

addSubview not working

// Create and add a sub view
CGRect viewRect = CGRectMake(0, 0, 200, 200);
UIView *a_sub_view = [[UIView alloc] initWithFrame : viewRect];

[window addSubview : a_sub_view];

After adding the above 开发者_Python百科3 lines of code, xcode produces no errors or warnings. But the sub-view is not showing at all. The sample program seems to be running exactly as before. Hope that somebody knowledgable could help.


I find it useful to set the background color so I know where the view is and the boundaries.

a_sub_view.backgroundColor = [UIColor redColor];

In your example, you're create an empty view so you won't 'see' anything.


window expects a viewcontrollers view to be added.

you can then add subviews to the current view.

so in your example use:

[self.view addSubview:a_sub_view];


I am using this in order to "find" the window:

UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
[window addSubview:myView];

This also worked for me, but it is a bit uglier:

[self.navigationController.tabBarController.view addSubview:myView];

To explain the second one, I had to "follow" the controllers back up to the "top" view. (My application has a tab bar with a navigation controller inside the current tab.)


If you are working with iOS 5 & above , you have to do as below:

[self.window.rootViewController.view addSubView:viewObject];


[self.view addSubView: a_sub_view];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜