Root view controller offset issue from Safe Area

867 Views Asked by At

When root view controller appears, the view seems like conflicting with safe area

enter image description here

But when I change tab and come back again to this tab again, is seems like everything is ok

enter image description here

Edit:

class Switcher {

static func updateRootVC(){
    
    let status = UserDefaults.standard.object(forKey: "Accesstoken")
    let userID = UserDefaults.standard.object(forKey: "UserId")
    let userName = UserDefaults.standard.object(forKey: "UserName")
    let userImage = UserDefaults.standard.object(forKey: "UserImage")

    if let currentUser = userID {
        requestManager.instance.userID = currentUser as! Int
    }
    if let currentStatus = status {
        requestManager.instance.getToken = currentStatus as? String
    }
    if let Name = userName {
        Api.Params.inputUserName = (Name as? String)!
    }
    if let Image = userImage {
        Api.Params.inputUserImage = (Image as? String)!
    }
    
    
    var rootVC : UIViewController?

    if(status != nil){
        rootVC = UIStoryboard(name: "Tabbar", bundle: Bundle.main).instantiateViewController(withIdentifier: "Tabbar") as! UITabBarController
    } else {
        rootVC = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "welcome") as! UINavigationController
    }
    
    rootVC!.view.insetsLayoutMarginsFromSafeArea = true


    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = rootVC
    appDelegate.window?.makeKeyAndVisible()
    
}}

Contraints of search items

user profile has the same top as search items.

enter image description here

TabViewController setup Code

    func setupTabbar(){
    if Api.Params.isGuest == true {
        let vc1 = storyboardTabbar.instantiateViewController(withIdentifier: "home") as! UINavigationController
        let vc2 = storyboardTabbar.instantiateViewController(withIdentifier: "favorite") as! GuestVC
        let vc3 = storyboardTabbar.instantiateViewController(withIdentifier: "scanner") as! ScannerVC
        let vc4 = storyboardTabbar.instantiateViewController(withIdentifier: "history") as! GuestVC
        let vc5 = storyboardTabbar.instantiateViewController(withIdentifier: "settings") as! GuestVC
        self.viewControllers = [vc1 , vc2 , vc3 , vc4 , vc5]
        self.selectedViewController = vc1

    } else if Api.Params.isLanguageChange == true{
        Api.Params.isLanguageChange = !Api.Params.isLanguageChange
        let vc1 = storyboardTabbar.instantiateViewController(withIdentifier: "home") as! UINavigationController
        let vc2 = storyboardTabbar.instantiateViewController(withIdentifier: "fav") as! UINavigationController
        let vc3 = storyboardTabbar.instantiateViewController(withIdentifier: "scanner") as! ScannerVC
        let vc4 = storyboardTabbar.instantiateViewController(withIdentifier: "his") as! UINavigationController
        let vc5 = storyboardTabbar.instantiateViewController(withIdentifier: "set") as! UINavigationController
        self.viewControllers = [vc1 , vc2 , vc3 , vc4 , vc5]
        self.selectedViewController = vc5
    } else {
        let vc1 = storyboardTabbar.instantiateViewController(withIdentifier: "home") as! UINavigationController
        let vc2 = storyboardTabbar.instantiateViewController(withIdentifier: "fav") as! UINavigationController
        let vc3 = storyboardTabbar.instantiateViewController(withIdentifier: "scanner") as! ScannerVC
        let vc4 = storyboardTabbar.instantiateViewController(withIdentifier: "his") as! UINavigationController
        let vc5 = storyboardTabbar.instantiateViewController(withIdentifier: "set") as! UINavigationController
        self.viewControllers = [vc1 , vc2 , vc3 , vc4 , vc5]
        self.selectedViewController = vc1
    } }
4

There are 4 best solutions below

2
On BEST ANSWER

After so many days , finally found out that top constraint to safeArea cause the issue , changing that to Superview done the trick

enter image description here

1
On

When you're setting your constraints, use safeAreaLayoutGuide

3
On

You can try this in your Switcher class, I hope it helps you

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let navigationController: UINavigationController?
let storyboard: UIStoryboard?;

if status != nil {

    storyboard = UIStoryboard(name: "Tabs", bundle: nil)

    // HomeTabBarController is a subclass of UITabBarController
    let vc = storyboard?.instantiateViewController(withIdentifier: "HomeTabBarController") as! HomeTabBarController
    navigationController = UINavigationController(rootViewController: vc)
} else {

    storyboard = UIStoryboard(name: "Main", bundle: nil)

    // MainViewController is a subclass of UIViewController
    let vc = storyboard?.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
    navigationController = UINavigationController(rootViewController: vc)

}

// This hides the navigationBar
navigationController?.navigationBar.isHidden = true

appDelegate.window?.rootViewController = navigationController
appDelegate.window?.makeKeyAndVisible()
1
On

Try specifying the view insets layout from safeArea using the insetsLayoutMarginsFromSafeArea property:

rootVC.view.insetsLayoutMarginsFromSafeArea = true