Search code examples
iphoneobjective-ciosipadios-universal-app

UIView in .XIB on Universal App always reports iPhone UI size


I am trying to port an iPhone app to iPad with as little work as possible and I am stuck on one last point.

I have a custom UI element that sits along the bottom of the screen. The UI element is simply a subclass of UIView with some delegate methods and custom drawing for my clients needs. The UIView contains several UIButton objects spaced out equally.

My question is how to get these programmatically created UIButtons to remain centred? There is a central .xib file that contains a UIView of subclass CustomNavBar. Within the CustomNavBar class I have tried to do the following:

    self.frame.size...

or

    self.bounds.size...

or

    CGRect rect = [[UIScreen mainScreen]bounds];
    rect.origin.size...

but all of the returns are the standard iPhone sizes. I have set all of the autoresizing on the other objects contained within the .XIB file but I am unable to fathom out what to do with the instance where the UI aspect is created programmatically.

Hope you can help.


Solution

  • You can use an alternate xib for iPad, so it has the default iPad size. If your xib is created for iPhone device family, it won't be well integrated on iPad.

    NSString* xibName = @"myview.xib";
    if ([[UIDevice currentDevice].model isEqualToString:@"iPad"])
    {
        xibName = @"myview_iPad.xib";
    }
    

    Use this code to get the iPad version of the xib for example. You can also create a macro (or function) that take the xib name and return either the iPhone xib name, or the iPad xib name (append the _iPad if the current device is an iPad).