Force set Landscape orientation is not working in xib

混江龙づ霸主 提交于 2019-12-08 12:33:26

问题


I have the following code

#define degreesToRadian(x) (M_PI * (x) / 180.0)
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){

    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.5f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    self.view.transform = CGAffineTransformIdentity;
    self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0));
    self.view.bounds = CGRectMake(0.0f, 0.0f, 1004, 768.0f);
    // self.view.center = CGPointMake(160.0f, 240.0f);

    [UIView commitAnimations];
}

This code will work fine for force setting landscape orientation

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

I am using the following code in viewdidload.This code is working fine in test app but in my mainapp.

if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
    {
        objc_msgSend([UIDevice currentDevice], @selector(setOrientation:),UIInterfaceOrientationLandscapeLeft);
    }
}

It is working fine if i write test application But in my Mainapp it is not working.I have xib in my Mainapp


回答1:


Make sure You have set Landscape mode for your Mainapp Application,

Edit

Than use Following method in Printview page ios5 and 6

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UIInterfaceOrientationIsLandscape( interfaceOrientation)) {
        return  YES;
    }
    return NO;

}

for ios 7

-(BOOL)shouldAutorotate
{
    if (UIInterfaceOrientationIsLandscape( [[UIDevice currentDevice] orientation])) {
        return  YES;
    }
    else
        return NO;
}



回答2:


In your project setting you need to check all the orientations that should be available in your app (even if you only use it once).

Then on your top most level object, usually a (Custom)UITabBar or (Custom)UINavigationBar, you have to implement some logic with these orientation methods:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

- (NSUInteger)supportedInterfaceOrientations

It think its best if you have a look for more detailed tutorials or answers on rotation.

iOS6+ Rotation

I don't know if your project also supports iOS5, but that version handles rotation different.




回答3:


I have written the following code in didRotateFromInterfaceOrientation:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation];

    NSLog(@"orientation %d",orientaiton);


    if(UIInterfaceOrientationIsPortrait(orientaiton)){
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
        {
            if(orientaiton == UIInterfaceOrientationPortrait)
                objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);
            else if (orientaiton == UIInterfaceOrientationPortraitUpsideDown)
                objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeRight);
        }
    }
}

I can rotate to landscape.but problem is view is rotating to portrait and then returning to landscape.But I don't want that.I want to fix the view to landscape.




回答4:


First you will set this from .xib

and then write this methods for that particular view

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}

and for another classes/views write Orientation for all.

NOTE:- you need to write these methods in all classes.



来源:https://stackoverflow.com/questions/21019908/force-set-landscape-orientation-is-not-working-in-xib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!