iosswiftscreen-rotationsfsafariviewcontroller

Fixing reposition of Done when rotating in SFSafariViewController


I am fairly new to iOS and am a bit stuck on this issue. The problem is that when I rotate the device from landscape to portrait in SFSafariViewcontroller the DONE button gets pushed up. See image here:

DoneImage

I thought about maybe recalculating the height and width in viewWillTransistion but I am honestly not sure what to do. The App is portrait only and only this ViewController should be rotated.

My Safari View Controller:

extension SFSafariViewController {
    convenience init(url URL: URL, entersReaderIfAvailable: Bool) {
        let configuration = SFSafariViewController.Configuration()
        configuration.entersReaderIfAvailable = entersReaderIfAvailable
        self.init(url: URL, configuration: configuration)
    }
}

public class MySafariViewController: SFSafariViewController {

    public var isIdleTimerDisabled: Bool = false

    public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        if size.height > size.width {
            Logging.debug("We are in portrait mode currently. Transitioning to landscape")
        } else {
            Logging.debug("We are in landscape mode currently. Transitioning to portrait")
        }
    }

    public override func viewDidLoad() {
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
        appDelegate.allowRotateAlways = true
        self.view.insetsLayoutMarginsFromSafeArea = true
    }

    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if isIdleTimerDisabled {
            UIApplication.shared.isIdleTimerDisabled = true
        }
    }

    public override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }

    public override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if isIdleTimerDisabled { // for the case if it's been true at the startup
            UIApplication.shared.isIdleTimerDisabled = false
        }

        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
        appDelegate.allowRotateAlways = false
    }

    public override var shouldAutorotate: Bool {
        return true
    }
} 

I am allowing rotation by setting the allowRotateAlways variabel to true. The variabel is used in AppDelegate in the func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {} function to change the orientation. So orientation change works i.e going from portrait to landscape and vice versa, but I am unsure why I get these side effects.


Solution

  • The solution was to embed the SafariViewController in a UINavigationController.