how to lock portrait orientation for only main view using swift

后端 未结 16 1932
野性不改
野性不改 2020-12-07 13:38

I have created an application for iPhone, using swift, that is composed from many views embedded in a navigation controller. I would like to lock the main v

16条回答
  •  广开言路
    2020-12-07 14:16

    If your iOS application is in Landscape mode only and You want to use camera in landscape mode of application then please try for below solution.

    Step 1:

    In your appdelegate.m class

    -(UIInterfaceOrientationMask)application:(UIApplication *)application
    supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
            NSString *captionVal = [TMUtils getValueInUserDefault:@"CAPTION"];
            if ([captionVal isEqualToString:@"Camera"]) {
                return UIInterfaceOrientationMaskPortrait;
            }else{
                return UIInterfaceOrientationMaskLandscapeRight;
            }
        }else{
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
    

    Here you can take shared preference value CAPTION as keyValue pair and store the value "Camera".

    Step 2: Now in your viewController.m class in camera button Action set shared preference value and open new ViewController which will be having camera functionality.

    [TMUtils setValueInUserDefault:@"CAPTION" value:@"Camera"];

    Step 3: In Camera functionality viewController.m class set storyboard with UIImageView and back button.

    Now in ViewDidLoad of camera functionality viewController.m set

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                NSLog(@"Error");
            } else {
    
                UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
                pickerController.modalPresentationStyle = UIModalPresentationCurrentContext; //this will allow the picker to be presented in landscape
                pickerController.delegate = self;
                pickerController.allowsEditing = YES;
                pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self presentViewController:pickerController animated:YES completion:nil];
            }
    }
    

    Now in UIImagePickerController delegate method set image to UIImageView

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
            UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
            self.cameraImage.image = chosenImage;
            [picker dismissViewControllerAnimated:YES completion:NULL];
    }
    

    Now on camera back UIButton

    - (IBAction)cameraBtnAction:(id)sender {
        [TMUtils setValueInUserDefault:@"CAPTION" value:@"NOCamera"];
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    

    It will always check according to shared preference value in delegate class function for supportedInterfaceOrientationsForWindow in reference to that value it allow camera functionality ViewController to open camera in Portrait mode and rest it will again go back to Landscape mode, which will completely work fine.

提交回复
热议问题