Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pushing a UIViewController in iOS 13 Causes it to Overlap Status Bar

Please note that this project was created in Xcode 10. I recently upgraded to Xcode 11 and this happened.

I have a UINavigationController that pushes the View Controllers. The initial view controller looks OK. But when I segue to another view controller by pushing it, the second view controller overlapped the status bar. This only happened in iOS 13. I tested on a simulator. Using Xcode 11.

The navigation bar's visibility is hidden.

Setting the Presentation to Full Screen or Current Context has the same result. It seems to go beyond the safe area, but I set it to be below the safe area.

I transitioned using:

self.navigationController?.performSegue(withIdentifier: "myAlertsSegue", sender: self)

This project was created in Xcode 10. I encountered this problem after updating to Xcode 11. If I create a new project in Xcode 11, there seems to be no problem. I'm guessing, I have a migration problem.

Here are some screenshots:

Initial view controller:

Initial View Controller

View Controller 2:

View Controller 2

Storyboard setup:

Xcode 11 storyboard

Segue Attributes Inspector:

Segue Attributes Inspector

View Controller 2 Attributes Inspector

View Controller 2 Attributes Inspector

Update The workaround I did was to embed each view controller to a navigation controller. It fixed the problem but the transition is not smooth because I cannot push navigation controllers. I had to present them. I don't know why is this happening on iOS 13.

Update I found the answer myself. See below if it helps you.

like image 835
meow2x Avatar asked Sep 27 '19 07:09

meow2x


1 Answers

I found the answer to my own question. I tried to create a new project in Xcode 11 to see the difference compared to a project created in Xcode 10. The differences are related to UISceneSession that is only available in iOS 13. I hope this answer helps all those who experience the same problem as I do.

Here the differences:

  • UISceneSession Lifecycle in the AppDelegate.swift There are methods for the UISceneSession. Add these lines in the AppDelegate.swift.
// MARK: UISceneSession Lifecycle

    @available(iOS 13.0, *)
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    @available(iOS 13.0, *)
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }
  • SceneDelegate.swift

This is a new class to be created.

import UIKit

@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
    }

    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }


}
  • And lastly, add this to the Info.plist
<key>UIApplicationSceneManifest</key>
    <dict>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneClassName</key>
                    <string></string>
                    <key>UISceneDelegateClassName</key>
                    <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
                    <key>UISceneConfigurationName</key>
                    <string>Default Configuration</string>
                    <key>UISceneStoryboardFile</key>
                    <string>Main</string>
                </dict>
            </array>
        </dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <false/>
    </dict>

Also, take note that when moving to another Storyboard, you might need to set modalPresentationStyle = .fullScreen.

Clean and build. Hope this helps. Upvote if it worked for you.

like image 134
meow2x Avatar answered Nov 04 '22 03:11

meow2x