how to lock portrait orientation for only view in ios 7

痞子三分冷 提交于 2019-12-23 20:35:44

问题


I have created an application for Iphone and Ipad that is composed from two main views with navigation controller. The navigation controllers are inserted into tabBar controller. I would like lock the main views to Portrait orientation and only a subview of a navigation controller trigger the possibility orintation to Partrait and Landscape. Is it possible? How Can I do?

Thanks


回答1:


Both answers given so far are wrong.

Here's what you do:

Make sure you list portrait and landscape in the list of supported orientations in your info.plist file. (The default app templates include all orientations for iPad, and all but portrait upside-down for iPhone, which is probably what you want.)

You want to implement the method supportedInterfaceOrientations: in the view controllers that you want to limit to portrait:

- (NSUInteger)supportedInterfaceOrientations
{
  return UIInterfaceOrientationMaskPortrait;
} 

Any view controllers that include that code will only support portrait. All others will support all the orientations listed in your info.plist.

The older method shouldAutorotateToInterfaceOrientation: is only needed if you support OS versions prior to 6.0.

You also only need to implement the method shouldAutorotate if you might return NO (don't rotate) sometimes.)




回答2:


Here is the universal approach based on the category for UIViewController with method swizzling.

//  UIViewController+Orientation.h
@interface UIViewController (Orientation)

- (void)setOrientation:(NSUInteger)orientation;

@end

//  UIViewController+Orientation.m
#import "UIViewController+Orientation.h"
#import <objc/runtime.h>

static NSUInteger __orientation = UIInterfaceOrientationMaskAll;

@implementation UIViewController (Orientation)

+ (void)load
{
    Method original, swizzled;
    original = class_getInstanceMethod(self, @selector(viewWillDisappear:));
    swizzled = class_getInstanceMethod(self, @selector(swizzled_viewWillDisappear:));
    method_exchangeImplementations(original, swizzled);
}

- (void)swizzled_viewWillDisappear:(BOOL)animated
{
    __orientation = UIInterfaceOrientationMaskAll;
    [self swizzled_viewWillDisappear:animated];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return __orientation;
}

- (void)setOrientation:(NSUInteger)orientation
{
    __orientation = orientation;
}

@end

Now you can fix orientation of any controller by setting orientation in viewWillAppear:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.orientation = UIInterfaceOrientationMaskPortrait;
}

Swizzling cares for autorelease orientation lock on controller change.

This approach works for all types of controllers including situation of TabbarViewController and UINavigationController which in fact reply to rotation events.

One can even make a lock button in the app which will get current orientation and set it as fixed via the category.




回答3:


Yes,Sure you can do this.

Below code force view controller for landscape, You can modify for portrait. i use it for iPhone.

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO];
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270));
    landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
    [[self navigationController].view setTransform:landscapeTransform];
    //NSLog(@"%f %f",self.view.frame.size.width,self.view.frame.size.height);

    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, self.width, self.height); 
    self.navigationController.navigationBar.frame = CGRectMake(0.0, y, self.width, 44.0);
    [appdelegate stopProgressIndicator];

}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0));
    landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0);
    [[self navigationController].view setTransform:landscapeTransform];
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, self.height, self.width);
    self.navigationController.navigationBar.frame = CGRectMake(0.0, y, self.height, 44.0);

}



回答4:


put this in all classes....

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

        if ([[[UIDevice currentDevice] systemVersion] integerValue]<7.0) {
            return UIInterfaceOrientationLandscapeRight|UIInterfaceOrientationLandscapeLeft;
        }else{
            return UIInterfaceOrientationPortrait;
        }
    }
    -(BOOL) shouldAutorotate {

        return NO;
    }
    -(NSUInteger)supportedInterfaceOrientations{
        return UIInterfaceOrientationMaskAll;
    }
    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{

        if ([[[UIDevice currentDevice] systemVersion] integerValue]<7.0) {
            return UIInterfaceOrientationLandscapeRight|UIInterfaceOrientationLandscapeLeft;
            return YES;
        }else{
            return UIInterfaceOrientationPortrait;
        }

        return NO;
    }


来源:https://stackoverflow.com/questions/23473704/how-to-lock-portrait-orientation-for-only-view-in-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!