将一个视图方向更改为横向

时间:2014-06-02 10:51:49

标签: ios objective-c device-orientation

我正在开展一个用户观看直播频道的项目。但我在这里面临一个小问题而且我已经很努力但却未能找到任何解决方案。我的应用程序将支持所有视图的纵向方向,但最后一个。最后一个视图仅支持横向。可能吗? 我搜索并尝试了以下代码

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

//

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

//

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation        {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);}

3 个答案:

答案 0 :(得分:1)

关注this link。希望你能在这里得到答案。

此链接显示如何将所有视图保持在纵向模式,但一个视图将处于横向状态。

您需要执行以下操作:

第一名:

在所有修复肖像的控制器中实现此功能:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

并为横向控制器实现此功能:

- (BOOL)shouldAutorotate
{
    return YES;
}


- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

第二名:

 // Fetch the status bar from the app and set its orientation as required. 
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

// Create an empty view controller, present it modally and remove it directly afterwards
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
// Now the device is rotated to the desired orientation. Go from there.

希望有效。

答案 1 :(得分:1)

从iOS6开始苹果改变了方向更新机制。从iOS6开始,iOS将仅向RootController报告方向事件,因此所有与方向相关的决策只能在RootController中进行。假设您正在使用UINavigationController或UITabBarController作为窗口的根控制器。在这种情况下,您可以创建UINavigationController或UITabBarController的子类,覆盖与方向相关的方法,并将方向相关事件传递给childControllers。将此自定义UINavigationController或UITabBarController对象设置为窗口的rootController。

您可以在自定义类中覆盖以下方法。

-(BOOL)shouldAutorotate
{
    BOOL shouldRotate = YES;
    if(self.viewControllers.count > 0)
        shouldRotate = [[self.viewControllers lastObject] shouldAutorotate];
    return shouldRotate;
}

-(NSUInteger)supportedInterfaceOrientations
{
    NSUInteger supportedInterfaces = UIInterfaceOrientationMaskAll;
    if(self.viewControllers.count > 0)
        supportedInterfaces = [[self.viewControllers lastObject] supportedInterfaceOrientations];
    return supportedInterfaces;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    UIInterfaceOrientation preferredOrientation = UIInterfaceOrientationPortrait;
    if(self.viewControllers.count > 0)
        preferredOrientation = [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
    return preferredOrientation;
}

答案 2 :(得分:0)

使用以下方法:在您的应用代理.h

@interface PlayWithWSWithLibAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

       BOOL flagOrientationAll;
}

@property (assign) BOOL flagOrientationAll;

在您的app delegate .m文件中添加以下方法

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    //NSLog(@"PlayWithWSWithLibAppDelegate -- supportedInterfaceOrientationsForWindow");
    if(flagOrientationAll == YES){
        return UIInterfaceOrientationMaskLandscapeRight;
    } else {
        return UIInterfaceOrientationMaskPortrait ;  // your Default orientation for all other view
    }
 }

在视图中实现以下方式,您希望在iPhone设备中以纵向和横向旋转

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.delegate = self;

    PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *) [[UIApplication sharedApplication] delegate];
    delegate.flagOrientationAll = YES;
 }
}

-(void)viewWillDisappear:(BOOL)animated
{
    //NSLog(@"viewWillDisappear -- Start");
     PlayWithWSWithLibAppDelegate *delegate = (PlayWithWSWithLibAppDelegate *)[[UIApplication sharedApplication] delegate];
        delegate.flagOrientationAll = NO;
}