[英]How to “lock” a view's orientation during device rotation
假設我有一個視圖控制器,稱為ViewControllerPortrait,它僅設計為以縱向模式顯示。 例如:
當用戶將設備旋轉為橫向時,我不希望ViewControllerPortrait再次將自身重新定向為橫向,但我確實希望呈現一個新的全屏視圖。 我們將其稱為全屏視圖控制器ViewControllerLandscapeFull。 例如:
我永遠不想看到的是:
我嘗試執行此操作的方法是讓窗口的rootViewController在獲取willRotateToInterfaceOrientation:duration:
時全屏顯示ViewControllerLandscapeFull willRotateToInterfaceOrientation:duration:
[[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
landscape | portrait
用於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。
我剛剛開始使用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;
}
houldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
如果上述方法位於Navigationcontroller的任何tabbarcontroller中,則不會調用viewcontroller。 如果在tabbarcontroller或導航控制器中聲明了這些方法,則將調用它們。 在我的情況下,視圖控制器位於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
並將其用於FixedOrientationTab
。 之后,如果您在viewcontroller中實現了shouldAutorotate
, supportedInterfaceOrientations
, preferredInterfaceOrientationForPresentation
這些方法,則將調用它們。
希望這可以幫助.. :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.