Ios 安全区域的根视图控制器偏移问题
当根视图控制器出现时,视图似乎与安全区域冲突 但当我换了标签,又回到这个标签上时,似乎一切都很好 编辑:Ios 安全区域的根视图控制器偏移问题,ios,swift,xcode,rootviewcontroller,Ios,Swift,Xcode,Rootviewcontroller,当根视图控制器出现时,视图似乎与安全区域冲突 但当我换了标签,又回到这个标签上时,似乎一切都很好 编辑: class Switcher { static func updateRootVC(){ let status = UserDefaults.standard.object(forKey: "Accesstoken") let userID = UserDefaults.standard.object(forKey: "UserI
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()
}}
搜索项目的限制条件
用户配置文件与搜索项目具有相同的顶部
选项卡视图控制器设置代码
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
} }
尝试使用以下属性从safeArea指定视图插入布局:
rootVC.view.insetsLayoutMarginsFromSafeArea = true
尝试使用以下属性从safeArea指定视图插入布局:
rootVC.view.insetsLayoutMarginsFromSafeArea = true
设置约束时,请使用SafeArealLayoutGuide设置约束时,请使用SafeArealLayoutGuide您可以在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()
你可以在你的切换器课上试试这个,我希望它对你有帮助
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()
经过这么多天,终于发现安全区域的顶部约束导致了问题,将其更改为Superview就成功了
过了这么多天,终于发现安全区域的顶部约束导致了问题,将其更改为Superview就成功了
请先尝试
让appDelegate=UIApplication.shared.delegate作为!AppDelegate
请先尝试让AppDelegate=UIApplication.shared.delegate作为!AppDelegate仍不工作:(,我已在故事板中标记已选中安全区域布局指南仍不工作:(,我已在Storyboard中选中了标记safeAreaLayoutGuide您可以与我们分享视图上部元素的约束,这些元素位于类别标签上方。我没有在布局约束中看到奇怪的内容,您已经在没有凹口的仿真器或Apple设备中尝试过验证是否存在相同的情况不会发生,例如iPhone 7或8?我可以推荐的另一件事是尝试为元素添加边框,每一个元素都有不同的颜色,这样您就可以直观地识别在执行过程中哪个元素被拉伸,并查看该元素是否有内容。我想这可能是因为我在tabViewControlle中的设置方式r,我正在添加TabViewController的屏幕截图..检查编辑器您可以与我们分享视图上部元素的约束,那些在类别标签上方的元素我看不到布局约束中有什么奇怪的东西,您已经在没有凹口的仿真器或Apple设备中试过验证相同不会发生,例如iPhone 7或8?我可以推荐的另一件事是尝试为元素添加边框,每一个元素都有不同的颜色,这样您就可以直观地识别在执行过程中哪个元素被拉伸,并查看该元素是否有内容。我想这可能是因为我在tabViewCon中的设置方式troller,我正在添加TabViewController的屏幕截图。检查编辑好,很好,很好