1

Can we create UIDeviceOrientationDidChangeNotification event manually?

In AppDelegate.swif I have

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask 

this method will return a result depends on user setting.

It work well but user need change setting during app running.

But the setting will be effective after user rotate the device, what I want is let it effective immediately

omar.zhou
  • 23
  • 1
  • 7

2 Answers2

0

Use NotificationCenter -

setupRotationListener()

/// Setup rotation listener
    func setupRotationListener() -> Void {
        NotificationCenter.default.addObserver(self, selector: #selector(self.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

    }

    /// Remove rotationListener
    func removeRotationlistener() -> Void {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)

    }

    /// Rotate camera
    func rotated() {
        //do your task 
 if UIDevice.current.orientation.isLandscape {
        print("Landscape")
    } else {
        print("Portrait")
    }
    }
AGM Tazim
  • 2,213
  • 3
  • 16
  • 25
0

I found a solution from stackoverflow

His solution is really solve my problem:

In iOS <= 4 and iOS >= 6:

UIViewController *vc = [[UIViewController alloc]init];
[self presentModalViewController:vc animated:NO];
[self dismissModalViewControllerAnimated:NO];
[vc release];

In iOS 5:

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *view = [window.subviews objectAtIndex:0];
[view removeFromSuperview];
[window addSubview:view];
omar.zhou
  • 23
  • 1
  • 7