如何以编程方式更改设备取向的iOS 6(How to change the device orien

2019-06-18 09:03发布

iOS 5中 ,我们可以以编程方式改变设备的方向,如下所示:

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

但在iOS 6中 setOrientation已被弃用,请问有什么可以编程改变设备方位的iOS 6?

Answer 1:

下面是我的“五毛钱”,在iOS7测试与ARC

[[UIDevice currentDevice] setValue:
                      [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                            forKey:@"orientation"];

这并不产生“泄漏”的警告,performSelector会。

UIAlertView中 - (?苹果,真的)使用此代码,当您查看时打开UIAlertView中(会/ DID)出现,你会发现所有,但这种观点是在人像我是不是能够迫使视图重新调整,但发现如果你把轻微的延迟打开UIAlertView中之前,然后查看有时间来改变方向。

注意我放开了自己的应用程序开始一周和2014年12月9日我将更新后是否会通过或失败。



Answer 2:

我发现,最简单的方法来迫使装置改变取向以呈现新视图控制器(使用presentViewController:animated:completion: ),其中新的视图控制器指定的特定优选取向(通过实施该方法-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation )。

当提出了一种新的视图控制器,如所预期,取向会改变到一个由新的视图控制器是优选的。 所以,最简单的实现(最佳实践?)会嵌入你需要在一个特定的方向到一个单独的视图控制器的所有功能,并在需要时出示。 该系统将改变方向为你的照顾。

显然,这可能不适合所有的情况,但幸运的是同样的招数适用于强制设备改变方向为现有的视图控制器。

关键是要提出与你所需要的具体的优选方向的新视图控制器,然后立即将其隐藏。 这将导致呈现新的视图控制器时的方向改变暂时的。 最好的部分是,当新的视图控制器被驳回,原来的(呈现)视图控制器的preferredInterfaceOrientationForPresentation再次查询,你可以指定你想在这里的最终方向。

在这里看出来了一个重要的事情是要在原有的视图控制器也临时关闭自动旋转(即将当从新近提出的,当时解雇视图控制器背面),这样当用户向新的方向转动自己的手机,它不触发进一步的自动旋转。

下面的代码应该说明我的观点,我的例子力量旋转为纵向,如果你希望其他方向只是发生相应的变化。

假设你有一个名为原始视图控制器Original ,并命名为一个临时视图控制器ForcePortrait

@interface Original : UIViewController
{
    BOOL orientationToPortrait; //should set to NO by default
}
@end

@implementation Original
- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation
{
    if(orientationToPortrait)
    {
        //when we manually changed, show in Portrait
        return UIInterfaceOrientationPortrait;
    }
    else
    {
        //before manual orientation change, we allow any orientation
        return self.interfaceOrientation;
    }
}

-(BOOL) shouldAutorotate
{
    //we should 'lock' the rotation once we manually change it
    return !orientationToPortrait;
}

-(void) changeOrientationToPortrait
{
    //Sample method to change the orientation
    //when called, will show (and hide) the temporary view
    //Original.preferredInterfaceOrientationForPresentation will be called again after this method

    //flag this to ensure that we tell system we prefer Portrait, whenever it asked again
    orientationToPortrait = YES;

    //presenting the following VC will cause the orientation to temporary change
    //when the new VC is dismissed, system will ask what is our (Original) orientation preference again
    ForcePortrait* forcePortrait = [[ForcePortrait alloc] init];
    [self presentViewController:forcePortrait animated:NO completion:^{
        [forcePortrait dismissViewControllerAnimated:NO completion:nil];
    }];
}


@end

@interface ForcePortrait : UIViewController
@end

@implementation ForcePortrait
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
@end


Answer 3:

这并没有回答如何改变设备的方向,但可能会帮助你的附加信息。

iOS 6的UI界面方向- shouldAutorotateToInterfaceOrientation:不工作

该方法shouldAutorotateToInterfaceOrientation:不支持的iOS 6。它弃用。 以防万一,如果你是一个新手,谁只是盯着可可工作,并想知道为什么你的视图控制器在iOS 6中搞砸了完善的iOS 5中,只知道shouldAutorotateToInterfaceOrientation:不支持了。 尽管它可能在Xcode 4起到了很好的4.3它不会在Xcode的4.5工作。

苹果提供了一种新的方法来让这件事做,在一个更清洁的方式。 您可以使用supportedInterfaceOrientations代替。 它返回所有视图控制器支持,接口取向值的掩模的界面取向。

UIInterfaceOrientationMask枚举:

这些常数是用于指定视图控制器的支持的接口取向屏蔽位。

typedef enum {
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
    UIInterfaceOrientationMaskLandscape =
        (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
    UIInterfaceOrientationMaskAll =
        (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
    UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
    UIInterfaceOrientationMaskAllButUpsideDown =
        (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
    UIInterfaceOrientationMaskLandscapeRight),
} UIInterfaceOrientationMask;

使用shouldAutorotateToInterfaceOrientation:方法:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsLandscapeRight(toInterfaceOrientation);
}

使用supportedInterfaceOrientations方法:

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscapeRight;
}

这些都是添加方法的UIViewController在iOS6的关于取向

  1. UIViewController中preferredInterfaceOrientationForPresentation

  2. UIViewController中shouldAutorotate

  3. UIViewController中supportedInterfaceOrientations

添加的方法来UIApplication的关于iOS6的方向

  1. UIApplication的supportedInterfaceOrientationsForWindow:

  2. UIInterfaceOrientationMask



Answer 4:

试试这个:

#import <objc/message.h>

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


Answer 5:

您应该将
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
在你的AppDelegate didFinishLaunchingWithOptions方法。

然后,在任何地方你的应用程序,你可以得到当前方位:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

而测试方位:

UIInterfaceOrientationIsPortrait(orientation) 
UIInterfaceOrientationIsLandscape(orientation)

因为,像

if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
    // code for landscape orientation
     // OR
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
     //  OR
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeLeft];

}
else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
    // code for Portrait orientation
    //  OR
   [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortraitUpsideDown];
    //  OR
   [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}


Answer 6:

这个代码是为iOS 8或更高

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];


Answer 7:

试试这个...它的工作对我来说...

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *view = [window.subviews objectAtIndex:0];
[view removeFromSuperview]; [window addSubview:view];


Answer 8:

@implementation的UINavigationController(自动旋转)

 -(NSUInteger)supportedInterfaceOrientations
 {
   //make the check for iphone/ipad here

    if(IPHONE)
    {
      return UIInterfaceOrientationMaskPortrait;
    } 
    else
    {
      return UIInterfaceOrientationMaskLandscape;
    }
 }

 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
 {
    return UIInterfaceOrientationPortrait;
 }

 - (BOOL)shouldAutorotate
 {
    return NO;
 }


Answer 9:

稍加修改,以Bissy的答案,如果你想避免使用运行时库:

if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]))
{
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
    {
        int orientationPortrait = UIInterfaceOrientationPortrait;
        NSMethodSignature *sig = [[UIDevice currentDevice] methodSignatureForSelector:@selector(setOrientation:)];
        NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];
        [invo setTarget:[UIDevice currentDevice]];
        [invo setSelector:@selector(setOrientation:)];
        [invo setArgument:&orientationPortrait atIndex:2];
        [invo invoke];
    }
}


Answer 10:

这适用于iOS7,力自动旋转为纵向。

//In your viewController.m
#import <objc/message.h>

// for autorotate viewController to portraid
- (void)viewWillAppear:(BOOL)animated {
    UIInterfaceOrientation orientationStatusBar =[[UIApplication sharedApplication] statusBarOrientation];
    switch (orientationStatusBar) {
        case UIInterfaceOrientationPortrait:break;
        case UIInterfaceOrientationLandscapeLeft:
            objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait);
            break;
        case UIInterfaceOrientationLandscapeRight:
            objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait);
            break;
        default:
            break;
    }
}

// this permit autorotate
- (BOOL) shouldAutorotate
{
   // this lines permit rotate if viewController is not portrait
    UIInterfaceOrientation orientationStatusBar =[[UIApplication sharedApplication] statusBarOrientation];
    if (orientationStatusBar != UIInterfaceOrientationPortrait) {
        return YES;
    }
    //this line not permit rotate is the viewController is portrait
    return NO;
}

注:我实现了我的应用程序这个选项,但可能会被苹果被拒绝(2012年十月奥斯汀的谢尔盖·K的编辑6评论)。



Answer 11:

-苹果在编程改变设备的方向iOS6的相当困难的(故意提醒你)。

据我所知,完成你的要求的唯一方法是模拟设备方向的变化。

使用setTransform旋转UIView ,并重新申请了自己的框架给出了预期的效果。

[YourView setTransform:CGAffineTransformMakeRotation(1.57)];
[YourView setFrame:CGRectMake(0, 0, YourView.frame.size.width, YourView.frame.size.height)];

而当设备的物理取向的变化,我们可以撤消的转变。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [YourView setTransform:CGAffineTransformMakeRotation(0)];
    [YourView setFrame:CGRectMake(0, 0, YourView.frame.size.width, YourView.frame.size.height)];
}


Answer 12:

if (self.interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
// http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation
// http://openradar.appspot.com/radar?id=697
// [[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight]; // Using the following code to get around apple's static analysis...
[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationLandscapeRight];
}


Answer 13:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{
    // Return YES for supported orientations
   return NO;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   return  interfaceOrientation == UIInterfaceOrientationPortrait
           || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ;
}


Answer 14:

这对我的作品在Xcode的6&5。

- (BOOL)shouldAutorotate {return YES;}
- (NSUInteger)supportedInterfaceOrientations {return (UIInterfaceOrientationMaskPortrait);}


文章来源: How to change the device orientation programmatically in iOS 6