1

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
    } }
Hanzala Raza
  • 149
  • 1
  • 16

4 Answers4

1

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

rootVC.view.insetsLayoutMarginsFromSafeArea = true
Claudio
  • 5,078
  • 1
  • 22
  • 33
1

When you're setting your constraints, use safeAreaLayoutGuide

iX0ness
  • 123
  • 9
1

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
    You can share us the constraints you have of the elements that are in the upper part of the view, those that are above Category label – Jorge Luis Peña Lopez Sep 19 '20 at 15:05
  • I do not see something strange in your layout constraints, you have tried in an emulator or Apple device that does not have Notch to verify that the same does not happen, example iPhone 7 or 8? The other thing I can recommend is to try to put a border to your elements, each one of a different color, so that you can visually identify which element is being stretched in size in execution and see if there is something with that element. – Jorge Luis Peña Lopez Sep 26 '20 at 23:23
  • i think it might be because of the way i am setting in tabViewController , im adding Screenshot of TabViewController .. check the edit – Hanzala Raza Sep 28 '20 at 06:05
1

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

Hanzala Raza
  • 149
  • 1
  • 16
  • 1
  • With this method you will get an unwanted margin on the top of the screen if the device doesn't have a notch. I had exactly the same problem and this was the solution that helped me: https://stackoverflow.com/a/50418550/575882 As it turns out, in the didFinishLaunchingWithOptions method the safe area is not set yet (keyWindow.safeAreaInsets.top is 0). So if you create the TabBarController and all its subviews here, the subview will be rendered on the whole screen. – gyimi Feb 08 '22 at 16:40