objective-c - 仅支持一个 View 的不同方向 iOS 6

标签 objective-c ios xcode

我只想在我的应用程序中将我的一个 View 旋转到向左横向或向右横向。我的所有其他 View 都处于纵向模式,并且我已将我的应用程序设置为仅支持纵向模式。随着 iOS 6 中方向的改变,我不知道该怎么做。我已经尝试了下面发布的内容。谁能告诉我我做错了什么?谢谢!

-(BOOL)shouldAutorotate {
return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
} 

我也试过:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didRotate:)
                                            name:UIDeviceOrientationDidChangeNotification
                                           object:nil];
return YES;//UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
} 


 -(void)didRotate:(NSNotification *)notification {
 UIDeviceOrientation orientation = [[notification object] orientation];

if (orientation == UIDeviceOrientationLandscapeLeft) {
    [theImage setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
} else if (orientation == UIDeviceOrientationLandscapeRight) {
    [theImage setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
    [theImage setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
} else if (orientation == UIDeviceOrientationPortrait) {
    [theImage setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
}
}

最佳答案

这对我有用 How to force a UIViewController to Portrait orientation in iOS 6

从 UINavigationController 覆盖旋转方法创建一个新类别:

-(BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

@end 

关于objective-c - 仅支持一个 View 的不同方向 iOS 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12772749/

相关文章:

ios - Facebook 登录无效范围

ios - 在WatchKit中解析查询

ios - 使用 ApI 在 ios 中阻止用户 in instagram

c# - 我可以在 Xamarin Forms 上检查设备中的可用存储空间吗?

ios - Xcode 试图定位或生成匹配的签名 Assets

ios - 如何在 xcode 7.3.1 swift 2.2 中使用 iOS 10 UserNotifications 框架?

objective-c - Objective-C 中的全局 C 字符串常量属性

objective-c - 即使没有 IV,AES CBC 中的 CCCrypt 解密也能正常工作

objective-c - 为什么变量不保留在 Asset-Library block 之外?

Xcode 4.1 无法在我的 Snow Leopard Mac OS X 10.6.3 上安装