A UIViewController created using initWithNibName: bundle: or through IBOutlet behaves differently

I have found strange behavior and would like to explain what statement I am doing wrong.

In the AppDelegate class of the newly created WindowBased project, I add a UIViewController to the window.
I can do this in two different ways:
 - with IBOutlet. In IB, I just installed the UIViewController, set my class to TestViewController and linked it (script A code).
 - creating a UIViewController with code (script B).

    - (void)applicationDidFinishLaunching:(UIApplication *)application {    

#define USE_IBOUTLET YES // Comment this line to switch to scenario B

#ifdef USE_IBOUTLET
    // Scenario A

    [window addSubview:theTestViewController.view];
    [window makeKeyAndVisible];
#endif


#ifndef USE_IBOUTLET
    // Scenario B

    TestViewController *theTestViewControllerProgrammatically;

    theTestViewControllerProgrammatically = [[TestViewController alloc] initWithNibName:nil bundle:nil];

    // According to Apple: "It is a good idea to set the view frame before adding it to a window.", so let do it
    [theTestViewControllerProgrammatically.view setFrame:[[UIScreen mainScreen] applicationFrame]];

    [window addSubview:theTestViewControllerProgrammatically.view];

    [window makeKeyAndVisible];
#endif
}

Since I did not configure any object in IB, I had to have the same behavior in both scenarios.

A IBOutlet , .
B :
 - ( 20 ).
 - (, )

?

, : http://dl.dropbox.com/u/1899122/code/ProtoWindowBasedStrangeness.zip

+3
3

, , , ().

:

[theTestViewController.view setFrame:[[UIScreen mainScreen] applicationFrame]];

:

[theTestViewControllerProgrammaticaly setFrame:[[UIScreen mainScreen] applicationFrame]];

VC, IB, , .

- , ! , , IB (, ).

+2

, , VC ! , , , , File Owner , . , IB.

, , . , , , ...

, Apple IB, . , , , IB VC , , UIViewController, , IB- , , IB UIViewController . , .xib "" , Apple , IB - , .

, MainWindow.xib IB VC, Inspector, "Resize View From NIB". , , VC , B. , File Owner ( UIViewController), , , , .

, TestViewController.xib VC , IB VC , UIViewController , , "Resize View From NIB" .

, IB, initWithNibName:bundle:nibBundle ( , ), , , ...

, , - !

+1

Probably, if in this representation it is not known about the presence of a status bar. You need to resize it accordingly and adjust its position to take into account the status bar. This is done by changing the properties of the frame (size) and borders (location) of the UIView.

0
source

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


All Articles