简体   繁体   中英

Want xib file to cover whole screen instead of covering top left ios

I am making an ios app and using xib files instead of storyboards. At first I didn't even have to use auto layout everything worked fine for all iphone devices in portrait. But as soon as I added a launch screen through assests only the the iphone 5 has the correct layout (because xib file size is set to 4 inch). But any greater screen size and only the top left hand corner is covered like below:

在此输入图像描述

I want the view to cover the whole screen. There is a view at the top but I cannot add any constraints to it. Inside the view there is an imageview that is supposed to cover the whole screen. So I added 4 constraints and pinned the 4 sides to 0 so the imageview should cover the whole screen. But it still does not work. What can I do so the whole screen will be covered?

Have you tried set the frame?

NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"xibFileName" owner:nil options:nil];        
[[array objectAtIndex:0] setFrame:self.view.frame]; // Trick here: set the main view frame

This worked in my case:

// Create a Container
    UIView *container = [[UIView alloc] initWithFrame:self.view.frame];
    container.backgroundColor = [UIColor clearColor];

// Add .xib file on top of the screen
    NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"xibFileName" owner:nil options:nil];        
    [container addSubview:[array objectAtIndex:0]];

// Add container to main view
    [self.view addSubview:container];

About the itens inside of .xib file you have to set the constraints there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM