简体   繁体   中英

iOS design orientation issue for Universal application

HI I am developing an iOS Universal application for both orientation Portrait and Landscape

I am using following code for adjusting my UI according to orientation change

- (void)viewDidLoad
{

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(deviceOrientationDidChange:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];


}

- (void)deviceOrientationDidChange:(NSNotification *)notification
{
    [self changeTheOrientation];
}

- (void)changeTheOrientation
{

    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
     {

        if ( UIDeviceOrientationIsPortrait(orientation))
        {
            //For Portrait set frame size
            lblBookInfo.frame = CGRectMake (0, 20, 718, 50);
            btnClose.frame = CGRectMake (718, 20, 50, 50);

        }
        else if (UIDeviceOrientationIsLandscape(orientation))
        {
            //For Landscape set frame size
            lblBookInfo.frame = CGRectMake (0, 20, 974, 50);
            btnClose.frame = CGRectMake (974, 20, 50, 50);

        }
    }
    else
    {

        if (UIDeviceOrientationIsPortrait(orientation))
        {
            //For Portrait set frame size

            lblBookInfo.frame = CGRectMake (0, 20, 290, 30);
            btnClose.frame = CGRectMake (290, 20, 30, 30);

        }
        else if (UIDeviceOrientationIsLandscape(orientation))
        {
            //For Landscape set frame size

             if ( [[UIScreen mainScreen] bounds].size.height==568 ) {


             }  else {


             }
        }  
    }
}

The basic code structure which I am using for handling different UI frame with change in orientation is above.

But the problem which i am facing is that the UIDeviceOrientationDidChangeNotification is not fired every time , I think it gets missed for some times , I have checked this by setting proper breakpoints , this problem makes look my Ui really bad as sometimes UI do not change accordingly to the frame.

This approach works properly for some initial UI changes but get failed after some (10-20)orientation changes.

Is it the correct approach which I am using or is there any better approach to handle different orientations in iOS

I am developing app for (iOS 6 and iOS 7).

Use the following code to detect orientation in the callback for the UIDeviceOrientationDidChangeNotification -

UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;

Then to detect if it is landscape or portairt you can -

if (UIDeviceOrientationIsLandscape(deviceOrientation) {
    //do landscape related view changes
} else if (UIDeviceOrientationIsPortrait(deviceOrientation) {
    //do portrait related view changes
}

I have seen this line being added before registering for orientation change notifications. Usually in awakeFromNib method -

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Then register for notifications -

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(deviceOrientationDidChange:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];

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