首页 文章

iOS:取消图像选择器后,方向更改为纵向

提问于
浏览
0

我有一个视图控制器,我想保持横向模式 . 我在课程中添加了以下代码:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask
    {
        return UIInterfaceOrientationMask.Landscape
    }

    func navigationControllerSupportedInterfaceOrientations(navigationController: UINavigationController) -> UIInterfaceOrientationMask
    {
        return UIInterfaceOrientationMask.Landscape
    }

    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation
    {
        return UIInterfaceOrientation.LandscapeLeft
    }

当我打开UIImagePickerController来选择照片时 . 如果我在选择器中单击取消,应用程序将以纵向模式返回到我的视图控制器 . 当我回到我的控制器时,我也试过设置方向,它只适用于第一次:

UIDevice.currentDevice().setValue(orientationBeforeShowingImagePicker.rawValue, forKey: "orientation")

1 回答

  • 0
    Finally. This worked for me. 
    
    @try 
    {
        // Create a UIImagePicker in camera mode.
        UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease]; 
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;  
        picker.delegate = self; 
    
        // Prevent the image picker learning about orientation changes by preventing the device from reporting them.
        UIDevice *currentDevice = [UIDevice currentDevice];
    
        // The device keeps count `enter code here`of orientation requests.  If the count is more than one, it continues detecting them and sending notifications.  So, switch them off repeatedly until the count reaches zero and they are genuinely off.
        // If orientation notifications are on when the view is presented, it may slide on in landscape mode even if the app is entirely portrait.
        // If other parts of the app require orientation notifications, the number "end" messages sent should be counted.  An equal number of "begin" messages should be sent after the image picker ends.
        while ([currentDevice isGeneratingDeviceOrientationNotifications])
            [currentDevice endGeneratingDeviceOrientationNotifications];
    
        // Display the camera.
        [self presentModalViewController:picker animated:YES];
    
        // The UIImagePickerController switches on notifications AGAIN when it is presented, so switch them off again.
        while ([currentDevice isGeneratingDeviceOrientationNotifications])
            [currentDevice endGeneratingDeviceOrientationNotifications];
    }
    @catch (NSException *exception) 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Camera" message:@"Camera is not available" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    

相关问题