微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。