iPhone以编程方式设置界面定位

我试图以编程方式更改单个UIViewController的方向。 如果视图是纵向的,用户应该在设置中select。 该值将被保存在NSUserDefaults ,在特定的ViewController ,方向应该被改变。 所以如果纵向上的interfaceOrientation应该在UIInterfaceorientationUpsideDown ……

我使用下面的方法,但只会改变方向,当用户将设备转向横向/纵向…,否则什么都不会触发。

 - (BOOL)shouldAutoRotateToInterfactOrientation:(UIInterfaceOrientation)interfaceOrientation 

此外,我tryied下一个(也与方法名称“ setOrientation ”):

 [[UIDevice currentDevice] orientation: UIDeviceOrientationPortraitUpsideDown]; 

但是,没有任何反应,只是出现错误,因为Xcode说没有这个名字的方法。

但是,它应该是另一种方式来改变它编程方式…第一种方法也只是一个方法,处理设备位置后的actionListener,但应该有一种方式来直接调用这个actionListener ….我只是看里面UIApplication文件,但没有有用的方法….

你应该使用这样的代码:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

然后,根据方向改变你的观点:

 -(void)rotationChanged:(NSNotification *)notification{ NSInteger orientation = [[UIDevice currentDevice] orientation]; UIWindow *_window = [[[UIApplication sharedApplication] delegate] window]; switch (orientation) { case 1: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.4]; [_window setTransform:CGAffineTransformMakeRotation (0)]; [_window setFrame:CGRectMake(0, 0, 320, 480)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES]; [UIView commitAnimations]; break; case 2: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.4]; [_window setTransform:CGAffineTransformMakeRotation (M_PI)]; [_window setFrame:CGRectMake(0, 0, 320, 480)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES]; [UIView commitAnimations]; break; case 3: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.4]; [_window setTransform:CGAffineTransformMakeRotation (M_PI / 2)]; [_window setFrame:CGRectMake(0, 0, 320, 480)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES]; [UIView commitAnimations]; break; case 4: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.4]; [_window setTransform:CGAffineTransformMakeRotation (- M_PI / 2)]; [_window setFrame:CGRectMake(0, 0, 320, 480)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES]; [UIView commitAnimations]; break; default: break; } } 

希望它有助于:P

iOS7和更早版本中使用此function是完美的。

 [[UIDevice currentDevice] setValue: [NSNumber numberWithInteger: UIInterfaceOrientationPortrait] forKey:@"orientation"]; 
 [[UIApplication sharedApplication] setStatusBarOrientation:yourDesiredOrientation]; 

然后,您将需要编辑shouldAutoRotate方法(以下示例适用于肖像:

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

从以前的回答:

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

警告

我没有可能评论这个答案,所以我会编辑它。

此答案的作者尝试将UIInterfaceOrientationtypes的值设置为UIDeviceOrientation的variables。 它们都是NSInteger枚举,所以编译器不会显示警告/错误。 但是它们有不同的含义,“器”还有“正视”,“正视”的方向。