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 three lines of code, xcode does not cause errors or warnings. But subview is not displayed at all. The sample program works exactly the same as before. Hope someone knows what might help.

+6
source share
5 answers

It’s useful for me to set the background color so that I know where the view and the borders are.

 a_sub_view.backgroundColor = [UIColor redColor]; 

In your example, you create an empty view so that you don't see anything.

+15
source
Window

awaiting viewing by viewcontrollers.

then you can add subitems to the current view.

so in your example use:

 [self.view addSubview:a_sub_view]; 
+3
source

I use this to β€œfind” a window:

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

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

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

To explain the second, I had to β€œfollow” the controllers to the β€œtop” view. (My application has a tab bar with a navigation controller inside the current tab.)

+3
source

If you work with iOS 5 and above, you need to do the following:

 [self.window.rootViewController.view addSubView:viewObject]; 
+2
source
 [self.view addSubView: a_sub_view]; 
-1
source

Source: https://habr.com/ru/post/890865/


All Articles