iOS: Presenting a view controller in landscape right from a view controller supporting only portrait mode after rotating the iphone

前端 未结 2 1435
广开言路
广开言路 2021-01-07 03:53

I have one app always presenting in Portrait mode (in the summary of the Xcode project, only the portrait orientation is supported).

Now what I want to do is when I\

2条回答
  •  清歌不尽
    2021-01-07 04:34

    I finally solved this problem, I suppose there are alternatives but this one works fine:

    In fact I kept only Portrait in the orientation restrictions. Then when I turn the phone in landscape right or left, I call my ARViewController modally, but before loading it I force this view controller to landscape (in viewWillAppear) by making an appropriate rotation like here:

    - (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self transformView2ToLandscape];}
    
    -(void) transformView2ToLandscape {
    
    NSInteger rotationDirection;
    UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
    
    if(currentOrientation == UIDeviceOrientationLandscapeLeft){
        rotationDirection = 1;
    }else {
        rotationDirection = -1;
    }
    
    CGAffineTransform transform = [arController.viewController.view transform];
    transform = CGAffineTransformRotate(transform, degreesToRadians(rotationDirection * 90));
    [arController.viewController.view setTransform: transform];}
    

    Edit: In Swift 4

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        transformViewToLansdcape()
    }
    
    func transformViewToLansdcape(){
        var rotationDir : Int
        if(UIDeviceOrientationIsLandscape(UIDevice.current.orientation)){
            rotationDir = 1
        }else{
            rotationDir = -1
        }
        var transform = self.view.transform
        //90 for landscapeLeft and 270 for landscapeRight
        transform = transform.rotated(by: (rotationDir*270).degreesToRadians)
        self.view.transform = transform
    }
    
    extension BinaryInteger {
        var degreesToRadians: CGFloat {
            return CGFloat(Int(self)) * .pi / 180
        }
    }
    

提交回复
热议问题