如何在Xcode中更改特定ViewController的方向

时间:2017-02-19 07:34:09

标签: ios objective-c xcode orientation

我想在Xcode中更改特定ViewController的方向。

我制作a,b,cViewController并将cViewController的方向更改为LandscapeRight。 (a和b的方向是肖像)

但是如果我在cViewController上更改方向并将ViewController从c移动到b,则b方向也会更改为LandscapeRight。 (屏幕转换是推送)

代码:

a和bViewController的DidLoad

int a[3]; is equivalent to int *const a;

so we cant change the array address location

int a[3]={1,2,3};

a++;//its an error

int b[3];

b=a;//its also an error

but int *c;

c=a;//Not an error because c is a pointer to integer not constant pointer to integer

similar to 
char str[10]="hello";

if i change like this

str="world";//its also an error

Pointers and arrays are always not equal, in some cases only like dereferencing 

   char a[10]="hello";
for(i=0;a[i]!='\0';i++)
   printf("%c",a[i]);

for(i=0;a[i]!='\0';i++)
   printf("%c",*(a+i));

cViewController的DidLoad

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

如何仅更改方向cViewController?

1 个答案:

答案 0 :(得分:1)

<强>步骤-1

在您的appdelegate中创建一个bool属性,例如,

@property () BOOL restrictRotation;

并调用函数

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
    return UIInterfaceOrientationMaskLandscape ;
else
    return UIInterfaceOrientationMaskPortrait;
}

<强>步骤-2

并在C VC中导入appdelegate #import "AppDelegate.h"

C VC视图上的

将出现,调用如

-(void)viewWillAppear:(BOOL)animated{
// for rotate the VC to Landscape
[self restrictRotationwithNew:YES];
}

(void)viewWillDisappear:(BOOL)animated{
   // rotate the VC to Portait
  [self restrictRotationwithNew:NO];

[super viewWillDisappear:animated];
}


-(void) restrictRotationwithNew:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;

}

选择2

C VC上的

使用UIDeviceOrientationDidChangeNotification

的委托功能检查方向
 [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification  object:nil];



 - (void)orientationChanged:(NSNotification *)notification{


     [self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];


}

- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation    {

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

switch (deviceOrientation) {
    case UIDeviceOrientationPortrait:

        NSLog(@"orientationPortrait");
       ;

        break;
    case UIDeviceOrientationPortraitUpsideDown:

        NSLog(@"UIDeviceOrientationPortraitUpsideDown");
        break;
    case UIDeviceOrientationLandscapeLeft:

        NSLog(@"OrientationLandscapeLeft");



        break;
    case UIDeviceOrientationLandscapeRight:

        NSLog(@"OrientationLandscapeRight");

        break;
    default:
        break;
}
}