我们说我有一个视图控制器,称之为ViewControllerPortrait,它只能以纵向模式显示。 E.g:
当用户将设备旋转到横向时,我不希望ViewControllerPortrait重新定位到横向,但我确实想要呈现一个新的全屏视图。让我们调用全屏视图控制器ViewControllerLandscapeFull。 E.g:
我不想看到的是:
我尝试这样做的方法是让窗口的rootViewController在获得willRotateToInterfaceOrientation:duration:
时全屏显示ViewControllerLandscapeFull
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[navigationViewController setNavigationBarHidden:YES animated:YES];
self.viewControllerPortrait = [[ViewControllerPortrait alloc] init];
self.viewControllerPortrait.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:self.viewControllerPortrait animated:NO completion:NULL];
然后在ViewControllerLandscapeFull中我有:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return (UIInterfaceOrientation)[UIDevice currentDevice].orientation;
}
这很有效。因为ViewControllerLandscapeFull"更喜欢"风景并且有UIModalPresentationFullScreen
modalPresentationStyle
,它只在风景中显示。通过返回landscape | portrait
for supportedInterfaceOrientations,允许设备旋转回纵向,当ViewControllerLandscapeFull获得willRotateToInterfaceOrientation:duration:
并最终调用dismissViewControllerAnimated:NO
时,会发生这种情况。
我唯一的问题是在旋转回到肖像时,你可以暂时看到横向的ViewControllerPortrait,这是我试图避免的。
ViewControllerPortrait确实实现了:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
我已经证实UIKit正在调用它,但它显然没有效果。 (文档说supportedInterfaceOrientations
仅在根视图控制器上调用,视图控制器全屏显示,所以我真的很惊讶UIKit完全调用它。)
FWIW,我正在使用iOS 8 beta 5 SDK并构建7/8。
Muchas gracias。
答案 0 :(得分:0)
我刚刚开始使用iPhone进行编程,但也许这会起作用......
点击您的项目文件>部署信息。然后取消除了肖像以外的一切。这样旋转设备就不会重新定位。
但是,您仍然可以使用以下方法测试设备是否旋转:
- (BOOL)shouldAutorotate
{
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
NSLog(@"ROTATE to landscape");
kitty.hidden = NO;
//(kitty is my imageview, hidden on load), once the device rotates to landscape, you can show your image.(or you can do something else)
}
else{
NSLog(@"ROTATE to portrait");
kitty.hidden = YES;
}
return YES;
}
答案 1 :(得分:0)
houldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
如果视图控件位于navigationcontroller的任何tabbarcontroller中,则上述方法不会被调用。如果在tabbarcontroller或导航控制器中声明了这些方法,那么它们将被调用。在我的例子中,viewcontrollers位于navigationcontroller中,导航控制器位于tabbarcontroller内。
为了解决这个问题,我创建了一个类FixedOrientationTab
,它是UITabBarController
的子类,以及一个导航类OrientationEnabledNavigation
,它是UINavigationController
的子类。然后,我在shouldAutorotate
和supportedInterfaceOrientations
中实施了preferredInterfaceOrientationForPresentation
,FixedOrientationTab
,OrientationEnabledNavigation
方法。
<强> OrientationEnabledNavigation.h 强>
#import <UIKit/UIKit.h>
@interface OrientationEnabledNavigation : UINavigationController
@end
<强> OrientationEnabledNavigation.m 强>
#import "OrientationEnabledNavigation.h"
@interface OrientationEnabledNavigation ()
@end
@implementation OrientationEnabledNavigation
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
// return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
<强> FixedOrientationTab.h 强>
#import <UIKit/UIKit.h>
@interface FixedOrientationTab : UITabBarController
@end
<强> FixedOrientationTab.m 强>
#import "FixedOrientationTab.h"
@interface FixedOrientationTab ()
@end
@implementation FixedOrientationTab
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
// return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
然后,如果您想在项目中使用导航控制器,请使用OrientationEnabledNavigation
和tabbar FixedOrientationTab
。之后,如果您在viewcontroller中实现了shouldAutorotate
,supportedInterfaceOrientations
,preferredInterfaceOrientationForPresentation
这些方法,那么它们就会被调用。
希望这会有所帮助.. :)