安全区域的根视图控制器偏移量问题

问题描述

出现根视图控制器时,该视图似乎与安全区域冲突

enter image description here

但是当我更改选项卡并再次回到该选项卡时,似乎一切正常

enter image description here

编辑:

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()
    
}}

搜索项约束

用户个人资料与搜索项的顶部相同。

enter image description here

TabViewController设置代码

    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,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,vc5]
        self.selectedViewController = vc1
    } }

解决方法

尝试使用insetsLayoutMarginsFromSafeArea属性从safeArea指定视图插图布局:

Text: 512.56,BigDecimal.toPlainString(): 512.56,double: 512,560000
,

设置约束时,请使用safeAreaLayoutGuide

,

您可以在Switcher类中尝试此操作,希望对您有帮助

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()
,

这么多天后,终于发现对safeArea的最大限制导致了问题,将其更改为Superview可以解决问题

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...