【发布时间】:2018-03-12 15:18:08
【问题描述】:
我试图只用一个视图来改变方向,其余的都固定在纵向上。我在我的 AppDelegate 中设置了一个方法,如下所示
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if globalVariables.gIsDosageView == "Y" {
if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
return UIInterfaceOrientationMask.all;
} else {
return UIInterfaceOrientationMask.portrait;
}
} else {
return UIInterfaceOrientationMask.portrait;
}
}
The global variable gIsDosageView is set to "Y" when the Dosage view is selected.我创建了两个独立的视图,portraitView (375x812) 和 LandscapeView (812x375)。我已经使用 viewWillTransition 方法来捕捉每个方向的变化,如下所示
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if size.width < 400 {
self.userImg.isHidden = false
self.deviceImg.isHidden = false
self.portraitView.isHidden = false
self.landscapeView.isHidden = true
}
else {
self.userImg.isHidden = true
self.deviceImg.isHidden = true
self.portraitView.isHidden = true
self.landscapeView.isHidden = false
}
}
当我从主屏幕转到 Dosage 时,它会显示正确的屏幕,并且会在两个方向视图之间切换而不会出现问题。但是,如果我转到另一个屏幕并返回“剂量”屏幕,它只会显示在两个方向屏幕中首次加载的屏幕。我已经浏览了代码,它隐藏了正确的屏幕,但这并没有反映在结果视图中。如果我先选择纵向屏幕,它将在纵向和横向之间成功切换,但如果我转到下一个屏幕并返回 Dosage,它只会显示纵向屏幕,无论方向如何,并且似乎忽略 viewWillTransition() 中的代码.
为什么会这样,我错过了什么?
【问题讨论】:
-
你没有打电话给
super.viewWillTransition(to:size, with:coordinator)。 documentation 提到这是必要的步骤。 -
viewWillTransition 每当方向改变时都会被调用,这是我想要的,所以我不需要调用它。问题是当它被调用时,视图并没有像它们应该在代码中那样隐藏
-
另外,你检查过你是在主线程中设置 UI 吗?
-
是的,我在代码中添加了一个 Dispatch.main.async() 块,结果是一样的
-
我想如果你在离开 Dosage 时更改
globalvariables.gIsDosageView,你需要在viewWillAppear中将其设置回“Y”。
标签: ios swift uiinterfaceorientation viewwilltransitiontosize