2015-09-29 32 views
4

我有一個視頻應用程序,但我想它僅停留在橫向在目標C中只使用景觀?

當景觀,它看起來是這樣的:

enter image description here

但是,當我轉身,它選擇縱向方向而像這樣:

enter image description here

也只是停留在橫向,已經試了又試,很多事情我已經看到,沒有任何工作,有人幫助我?

[更新]

只需要一個視圖中不使用方向縱向,有人已經有這個問題嗎?

有沒有人有任何其他想法?沒有在這裏工作

[更新] - > 30/09/2015

我的解決方案:

在AppDelegate.m我用這個:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    if(self.restrictRotation == YES) 
     return UIInterfaceOrientationMaskLandscape; 
    else 
     return UIInterfaceOrientationMaskAll; 
} 

AppDelegate.h:

@property() BOOL restrictRotation; 

我想在哪裏只留下山水,我用它(videoViewController.m):

在viewDidLoad中

[self restrictRotation:YES]; 

     NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight]; 
     [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 

,並添加方法:

-(void) restrictRotation:(BOOL) restriction 
{ 
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    appDelegate.restrictRotation = restriction; 
} 

記得,如果在videoViewController.h的#進口 「AppDelegate.h」

並且您不想要的視圖鎖定在橫向添加:

在viewDidLoad中:

[self restrictRotation:YES]; 

和方法:

-(void) restrictRotation:(BOOL) restriction 
    { 
     AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
     appDelegate.restrictRotation = restriction; 
    } 

問題解決了! THX所有

+0

請指定*你已經嘗試了什麼,也許還需要添加一些代碼。 – Numbers

+0

我現在想要的是: - (void)viewDidAppear:(BOOL)animated {super viewDidAppear:animated]; NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; [[UIDevice currentDevice] setValue:value forKey:@「orientation」]; } –

+0

這是爲了強制旋轉設備旋轉,如果您按照我在答案中提到的步驟操作,則可能不需要此操作。 – baydi

回答

2

您可以在2個可能的方式

1)enter image description here

2)做到這一點通過編程

-(BOOL)shouldAutorotate{ 
return YES; 

} 
-(NSUInteger)supportedInterfaceOrientations{ 
     return UIInterfaceOrientationMaskLandscape; 
} 

爲此,你需要它看起來你在你做NavigationController類正在使用NavigationBar

如果沒有,那麼用它在你的ViewController

+0

我也試過,但是我錯了嗎?我在ViewController中使用 –

+0

如果您使用的是UINavigationBar,那麼您必須創建一個CustomNavigationController類並從UINavigationController繼承該類,並在該類中使用此代碼,還有一件事使用該CustomNavigationController而不是UINavigationController – baydi

+0

您只需要移除旁邊的檢查在設置中的肖像方向。不要添加代碼。它將適用於您應用中的所有控制器。 – hasan83

0

您可以設置變換角度播放器屏幕/播放器父視圖以橫向模式始終顯示。

添加此代碼塊在視圖中加載方法顯示在橫向模式。

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 
    [[self view] setBounds:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)]; 
    CGFloat radians = atan2f(self.view.transform.b, self.view.transform.a); 
    if (radians!=M_PI/2) { 
     [[self view] setTransform:CGAffineTransformMakeRotation(M_PI/2)]; 

    } 
} 

並添加此方法在設備旋轉時總是以橫向模式顯示。

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ 


UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 

if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { 
    [[self view] setBounds:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)]; 
    CGFloat radians = atan2f(self.view.transform.b, self.view.transform.a); 
    if (radians!=M_PI/2) { 
     [[self view] setTransform:CGAffineTransformMakeRotation(M_PI/2)]; 
    } 



}else{ 

    [[self view] setBounds:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    CGFloat radians = atan2f(self.view.transform.b, self.view.transform.a); 
    if (radians!=M_PI*2) { 
     [[self view] setTransform:CGAffineTransformMakeRotation(M_PI*2)]; 
    } 

} 

}

+0

在哪能添加代碼? –

+0

當你啓動你的播放器播放視頻時,你可以添加代碼 –

+0

沒有工作,有其他的想法?添加ViewDidLoad –

0

在你AppDelegate.m添加這個方法:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

此方法確定哪個接口方位支持的應用廣泛,因此,如果您返回(UIInterfaceOrientationMaskLandscape)應用程序將工作只有在橫向模式下,不管在任何內容視圖控制器中支持的方向,以及在任何內容中支持的方向視圖控制器應該只是AppDelegate.m中支持的方向的一個子集。

,如果你想在橫向到一個視圖控制器被迫只,然後在AppDelegate.m:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    return UIInterfaceOrientationMaskAll; 
} 

,並在視頻VC:

-(UIInterfaceOrientationMask)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

最後呈現視頻VC使用:

[self presentViewController:videoVC animated:YES completion:nil]; 
+0

問題是我需要只有視頻視圖是風景 –

+0

錯誤:終止應用程序由於未捕獲的異常'NSInvalidArgumentException',原因:'應用程序試圖以模態方式呈現活動控制器

+0

我使用:viewDidLoad - > [self presentViewController:self.playerView動畫:YES完成:無];和 - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } 在我的ViewController –