简体   繁体   English

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

[英]change one view orientation to landscape

I'm working on a project in which user will watch live channels. 我正在一个项目中,用户将观看直播频道。 But I'm facing a small problem here and I've tried very hard but failed to find any solution. 但是我在这里面临一个小问题,我已经非常努力,但是没有找到任何解决方案。 My app will support portrait orientation for all views but last one. 我的应用程序将支持除最后一个视图外的所有视图的纵向方向。 Last view will support only landscape orientation. 最后一个视图仅支持横向。 Is it possible? 可能吗? I've searched and tried following code 我已经搜索并尝试了以下代码

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

// //

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

// //

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

Follow this link . 点击此链接 Hope you get your answer here. 希望你在这里得到答案。

The link shows how to keep all your views in portrait mode except one view that will be in landscape. 该链接显示了如何将所有视图保持为纵向模式,只有一个视图处于横向模式。

You need to do the following: 您需要执行以下操作:

1st : 第一:

Implement this in all controllers that are fix for portrait: 在所有适合纵向的控制器中实现此功能:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

And implement this for the landscape controllers: 并为景观控制器实现此功能:

- (BOOL)shouldAutorotate
{
    return YES;
}


- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

2nd : 第二名

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

Hope that works. 希望能奏效。

From iOS6 apple has changed orientation update mechanism. 苹果从iOS6开始改变了方向更新机制。 From iOS6 onwards, iOS will report orientation event only to RootController, so all the orientation related decision can only be taken in RootController. 从iOS6开始,iOS将仅向RootController报告方向事件,因此所有与方向相关的决定只能在RootController中做出。 Lets say you are using UINavigationController or UITabBarController as window's root controller. 假设您使用UINavigationController或UITabBarController作为窗口的根控制器。 In this case you can create sub class of UINavigationController or UITabBarController, override orientation related methods and pass orientation related events to childControllers. 在这种情况下,您可以创建UINavigationController或UITabBarController的子类,重写与方向相关的方法,并将与方向相关的事件传递给childControllers。 Set this custom UINavigationController or UITabBarController object as rootController of your window. 将此自定义UINavigationController或UITabBarController对象设置为窗口的rootController。

You can override below methods in your custom class. 您可以在自定义类中重写以下方法。

-(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;
}

Use Following approach : In your app delegate .h 使用以下方法:在您的应用程序委托.h中

@interface PlayWithWSWithLibAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

       BOOL flagOrientationAll;
}

@property (assign) BOOL flagOrientationAll;

Add following method in your app delegate .m file 在您的应用程序委托.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
    }
 }

Implement following way in your view which you want to rotate in both portrait and landscape both for iPhone device 在您的视图中实现以下方法,您想同时针对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