简体   繁体   中英

Convert iPhone app to universal binary

I have an iPhone game and I want to convert the project into a universal one. In fact, I have done so already by adjusting the target. The problem is: For example the UIButtons don't resize and don't relocate.

Question: What is the correct way to create a button so that it automatically adjusts its size if the universal app is run on the Pad?

At the moment I'm making buttons like so:

    [self setSettingsButton:[UIButton buttonWithType:UIButtonTypeCustom]];
    [settingsButton setFrame:CGRectMake(38, 121, 30, 180)];
    [settingsButton setBackgroundImage:[UIImage imageNamed:@"SettingsButton.png"] forState:UIControlStateNormal];
    [settingsButton addTarget:self action:@selector(prefButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:settingsButton];

Well yes, obviously, the positions and sizes are absolute when they should be relative but my question is: how to make them relative ?

Many thanks for your help!

I see two ways in this situation:

1)

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    [settingsButton setFrame:CGRectMake(38, 121, 30, 180)];
}
else {
    [settingsButton setFrame:CGRectMake(131, 145, 60, 250)]; // your iPad values
}

2)

CGRect bounds = self.view.bounds; // Here we get VC views frame size
CGRect buttonFrame = CGRectZero;
buttonsFrame.origin.x = bounds.size.width * 1.0/3.0;   // Yours calculations
buttonsFrame.origin.y = bounds.size.width * 4.0/5.0;   //
buttonsFrame.size.width = 240;                         //
buttonsFrame.size.height = 300;                        //
[settingsButton setFrame:buttonsFrame];

Hope this'll help!

You can go the if/else route zapko suggests or you can set the main window for each device in the project settings. This will at least allow you to isolate the code for the device outside an if/else block and in my experience it is cleaner to maintain.

Have you also accounted for the Retina display? I would think the same issue would hit when you test on the standard display vs. the Retina display as well. Since you are creating a game this may not be an option, but adding the buttons to your view via IB will give the app the ability to scale across screens.

最好为iphone和ipad进行设计是最佳选择。否则,必须检查设备是iphone还是ipad([UIDevice currentDevice] .model),然后为两者设置框架

if ([UIDevice currentDevice].model == iPhone) {
    [settingsButton setFrame:CGRectMake(38, 121, 30, 180)];
}
else {
    [settingsButton setFrame:CGRectMake(131, 145, 60, 250)];
}

or

create a universal app you have to design for both iphone page as well as ipad page.

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