繁体   English   中英

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

[英]change one view orientation to landscape

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

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

//

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

//

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

点击此链接 希望你在这里得到答案。

该链接显示了如何将所有视图保持为纵向模式,只有一个视图处于横向模式。

您需要执行以下操作:

第一:

在所有适合纵向的控制器中实现此功能:

- (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.

希望能奏效。

苹果从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;
}

使用以下方法:在您的应用程序委托.h中

@interface PlayWithWSWithLibAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

       BOOL flagOrientationAll;
}

@property (assign) BOOL flagOrientationAll;

在您的应用程序委托.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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM