开发者

UIView is shown displaced to the top

开发者 https://www.devze.com 2023-02-17 08:35 出处:网络
I have a simple app that has two view controllers. Both of them have a UINavigationBar at the top, as a header.The second UIViewController is displayed as a modal view, when the user clicks on a butto

I have a simple app that has two view controllers. Both of them have a UINavigationBar at the top, as a header. The second UIViewController is displayed as a modal view, when the user clicks on a button on the first one.

When my app first launches, the initial view doesn't completely cover the main UIView and seems "pushed" to the top (see image below).

UIView is shown displaced to the top

After I click on the "instructions" button, which displays another view with presentModalViewController:animated:, and dismiss the modal ViewController, everything is displayed correctly.

Anybody knows what I might be doing wrong?

开发者_Python百科

I have nothing in viewWillAppear, and this is my viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];

    if (!self.model) {
        self.model = [[FRRSushiRiceModel alloc] init];
        [[self.header.items objectAtIndex:0] setTitle:  @"Perfect Sushi Rice: Ingredients"];
    }
}

and my application:didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


    // Create and add the main controller (ingredients)
    self.ingredientsController = [[FRRIngredientsViewController alloc] init];
    [window addSubview:self.ingredientsController.view];


    [window makeKeyAndVisible];

    return YES;
}

This small project reproduces this behavior: Test Case


Did you untick the "Wants Full Screen" setting in IB, either for the UINavigationController or UIViewController?

UIView is shown displaced to the top


I found the error, guys.

Basically I was trusting the system to correctly set the frame of my views to match the usable portion of the screen. This works when you add it to some controller of controllers (such as UINavigationController), or add it via IB.

If you add your controllers programmatically, you need to set the view's frame explicitly. A good default is:

[[UIScreen mainScreen] applicationFrame] 

represents the part of the screen available to applications: the whole screen minus the status bar.

0

精彩评论

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