安全区域的根视图控制器偏移问题
Root view controller offset issue from Safe Area
当根视图控制器出现时,视图似乎与安全区域冲突
但是当我切换标签并再次回到这个标签时,似乎一切正常
编辑:
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()
}}
搜索项的限制条件
用户个人资料与搜索项的排名相同。
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 , 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
} }
尝试使用 insetsLayoutMarginsFromSafeArea 属性:
从 safeArea 指定视图插入布局
rootVC.view.insetsLayoutMarginsFromSafeArea = true
设置约束时,请使用 safeAreaLayoutGuide
您可以在您的 Switcher 中尝试此操作 class,希望对您有所帮助
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 就成功了
当根视图控制器出现时,视图似乎与安全区域冲突
但是当我切换标签并再次回到这个标签时,似乎一切正常
编辑:
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()
}}
搜索项的限制条件
用户个人资料与搜索项的排名相同。
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 , 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
} }
尝试使用 insetsLayoutMarginsFromSafeArea 属性:
从 safeArea 指定视图插入布局rootVC.view.insetsLayoutMarginsFromSafeArea = true
设置约束时,请使用 safeAreaLayoutGuide
您可以在您的 Switcher 中尝试此操作 class,希望对您有所帮助
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 就成功了