Sonic Master
Sonic Master

Reputation: 1248

Change .xib size on different iPad screen resolution programmatically

I have a problem displaying HUD in a view of iPad mini/iPad 4 and iPad Pro. It won't go centre as it should be. And I think, it was a problem with my .xib size. It depends on simulated metric setting.

I try to set .xib using simulated metric,

1. iPad Full Screen

I set my .xib with iPad Full Screen,

iPad

And if I opened as source code, it gives me 1024 x 704 resolution.

<rect key="frame" x="0.0" y="64" width="1024" height="704"/>

Then I run this on iPad, and my HUD is centered as it should be. But if I run on iPad Pro, my HUD doesn't centre as well. It looks like my HUD still at centre of 1024 x 704.

2. iPad Pro Full Screen

I try to set .xib with iPad Pro Full Screen

iPad Pro

And it gives me 1366 x 960 resolution

<rect key="frame" x="0.0" y="64" width="1366" height="960"/>

Then I run this on iPad Pro, and my HUD is centered as it should be. But if I run on iPad, my HUD doesn't centre as well. It looks like my HUD still at centre of 1366 x 960.

I don't think that simulated metric's setting impacts on my HUD problem as it says here. I am using JGProgressHUD btw, and here is how I showed my HUD.

HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
HUD.textLabel.text = NSLocalizedString(@"MISC_LOADING_DATA", nil);
[HUD showInView:self.view];

This HUD will centre automatically at self.view and I think it was my .xib size that matters. My question is, how should I set .xib size that will centre my HUD on iPad and iPad Pro?

Any help would be appreciated. Thank you.

UPDATE

My problem is self.view.bounds.size value on viewDidLoad is different with self.view.bounds.size on some method's completion handler.

- (void)viewDidLoad {
    [super viewDidLoad];

    // first print
    NSLog(@"Height: %f", self.view.bounds.size.height);
    NSLog(@"Width: %f", self.view.bounds.size.width);

    [self generateData:^(BOOL result) {
        if (HUD.superview) {
            [HUD dismiss];

            // 2nd print on completion handler
            NSLog(@"Height: %f", self.view.bounds.size.height);
            NSLog(@"Width: %f", self.view.bounds.size.width);
        }
    }];
}

I run on iPad Pro. On first print, it gives me

2016-05-19 16:39:31.529 hr-akses[34013:1535416] Height: 704.000000
2016-05-19 16:39:31.529 hr-akses[34013:1535416] Width: 1024.000000

But on 2nd print, it gives me

2016-05-19 16:39:31.607 hr-akses[34013:1535416] Height: 960.000000
2016-05-19 16:39:31.607 hr-akses[34013:1535416] Width: 1366.000000

. .

SOLVED

As Luke Smith said, autolayout is most likely not finished doing its stuff at viewDidLoad. I initialise my HUD on viewWillAppear and it solving my problem.

Upvotes: 0

Views: 645

Answers (1)

Luke Smith
Luke Smith

Reputation: 1320

This size setting you are playing with here is purely for the size of the editing screen in Interface Builder. Your problem here is not with this setting, but with your autolayout rules, which you have not mentioned.

Try these rules for example : to fix it 4 pixels from the top, and give it a fixed width, and height, you need this :

Fix 4 px from top, and fix width and height

Then to fix it so that it is always centred horizontally, you need this :

Centre item horizontally in container

(Note carefully the ticks in the boxes, and the red marks on the position constraints - those are the ones being created there)

Now with those 4 rules in place, your item is always centred horizontally, 4 from the top, and a set width and height, on absolutely any device, from the watch to the iPad Pro.

If you now change the setting for Size in Simulated Metrics, yes the IB screen might give some errors, but the same rules will still be in place, and will still work at runtime. The errors are simply with the layout shown in Interface Builder - ie, the rules say the item should be dead centre, but right now it isnt.

What you can do if the IB screen still shows errors, is to select the screen item that has the positioning errors, and do one of two things : Update frames / contraints

Updating frames : if you select this, it takes the rules, and applies them as if the app was running. So if the rules say that it should be centred, and it is not currently centred in Interface Builder, then selecting this will do that centring.

Updating constraints : If you select this, IB assumes that the position of the item is actual right (if it isnt centred for eg) and rather than changing the position of the item, instead the rules themselves are changed to suit the position the item is currently in.

Have a play with those so that it sinks in - this stuff is really handy and used all the time.

Upvotes: 2

Related Questions