![](/img/trans.png)
[英]Rotate Landscape to Portrait in MPMoviePlayerViewController in ios6 and ios7
[英]iOS7 / iOS6 Conditional Rotation Portrait / Landscape for different sections of App
問題: A有一個應用程序,它對應用程序的不同部分使用橫向模式(鎖定)和縱向模式(鎖定)。 現在我有一個可行的解決方案,但它似乎不正確並且確實存在問題。
最理想的是,我希望強制改變方向。 甚至考慮在需要時進行視圖轉換。
App的基本流程:
注意:
基本圖像顯示了不同視圖方向的外觀。 (線條表示app的流程,圖像的方向表示每個屏幕應該如何)
目前使用以下實現調用/設置如果視圖處於縱向模式或橫向模式[setLockedToPortait:YES](用於縱向視圖)等。
這個術語可以查詢在旋轉設備時從iOS使用的界面方向。
現在,對於轉到LandscapeView的情況,我在普通視圖的頂部顯示一個臨時視圖,要求使用它將手機旋轉到橫向。 (從橫向視圖返回HomeView時也會顯示臨時視圖)
因此,一旦用戶旋轉了他們的設備,它將觸發正確的方向,然后臨時視圖將隱藏。
如果用戶此時將手機旋轉回肖像,它仍將被鎖定為橫向,因此不會觸發另一個視圖旋轉(也不會出現臨時視圖或任何內容)
現行執行守則::
// ---------------------- NavigationController (subclass of UINavigationController)
@interface NavigationController () {
BOOL isOrientationPortrait;
}
@end
@implementation NavigationController {
UIDeviceOrientation lastAccepted;
UIDeviceOrientation lastKnown;
}
-(void)setLockedToPortait:(BOOL)isLocked {
isOrientationPortrait = isLocked;
}
-(UIDeviceOrientation) getCurrentOrientation {
UIDeviceOrientation orientate = [[UIDevice currentDevice] orientation];
if(orientate == 0) { // needed for simulator
orientate = (UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation;
}
return orientate;
}
// Deprecated in iOS6, still needed for iOS5 support.
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
{
UIDeviceOrientation orientation = [self getCurrentOrientation];
[self setLastKnownOrientation:orientation];
if(isOrientationPortrait == YES) {
if([self isLastKnownPortrait] == YES) {
[self setLastAcceptedOrientation:orientation];
return YES;
} else {
return NO;
}
} else {
if([self isLastKnownLandscape] == YES) {
[self setLastAcceptedOrientation:orientation];
return YES;
} else {
return NO;
}
}
}
// iOS6/7 support
- (BOOL)shouldAutorotate
{
// find out the current device orientation
UIDeviceOrientation orientation = [self getCurrentOrientation];
[self setLastKnownOrientation:orientation];
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if(isOrientationPortrait == YES) {
if([self isLastKnownPortrait] == YES)
{
UIDeviceOrientation orientation = [self getCurrentOrientation];
[self setLastAcceptedOrientation:orientation];
}
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
} else {
if([self isLastKnownLandscape] == YES)
{
UIDeviceOrientation orientation = [self getCurrentOrientation];
[self setLastAcceptedOrientation:orientation];
}
return (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight );
}
}
-(void)setLastAcceptedOrientation:(UIDeviceOrientation)orient {
lastAccepted = orient;
}
-(void)setLastKnownOrientation:(UIDeviceOrientation)orient {
lastKnown = orient;
}
-(BOOL)isLastKnownPortrait {
return UIDeviceOrientationIsPortrait(lastKnown);
}
-(BOOL)isLastKnownLandscape {
return UIDeviceOrientationIsLandscape(lastKnown);
}
-(BOOL)isLastAcceptedPortrait {
return UIDeviceOrientationIsPortrait(lastAccepted);
}
-(BOOL)isLastAcceptedLandscape {
return UIDeviceOrientationIsLandscape(lastAccepted);
}
目前的問題:
希望能夠:
我在這里以及Apple Dev論壇上看了很多其他解決方案,但似乎沒有人能夠解決這個問題,或者兩個視圖之間的這種方向錯誤仍然存在。
感謝您的幫助或指點! 沒有建議可以打折:D
- 編輯::
因此,不要試圖強制改變視圖的方向。 只需推送一個新的模態視圖。 這迫使改變。 您仍需要使用上方的方向代碼來管理旋轉。
那么我現在在HomeViewController中有什么:
LandscapeViewController * viewController = [[[LandscapeViewController ViewController alloc] init] autorelease];
UINib * nib = [UINib nibWithNibName:@"NavigationController" bundle:nil];
NavigationController *navController = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];
[navController initWithRootViewController:viewController];
[self presentViewController:navController animated:YES completion:^{
// completion
}];
因此有必要為此模態視圖重新添加新的導航控制器。 另請注意,'presentViewController'是推送Modal視圖的新方法。
實現了這個重載方法來管理視圖控制器:
-(id)initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithRootViewController:rootViewController];
if(self){
}
return self;
}
注意:上面沒有使用故事板。 可以通過使用故事板並以相同方式模態地顯示視圖來解決該問題。
見我的答案在這里 ,包括一個測試項目。
基本上,只有在以模態方式呈現視圖控制器時才能強制改變方向。 例如,某些應用中的媒體播放。 如果您希望從只能以縱向顯示的視圖控制器轉換為僅以橫向顯示的視圖控制器,則需要進行模式演示。 推送不起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.