【发布时间】:2023-03-13 07:30:01
【问题描述】:
我有一个适用于 iOS 的应用程序,它有 3 个容器视图(A、B 和 C)。顶部容器视图 (A) 是静态的,下面是一个容器视图 (B),带有一个按钮,可将其“移动”到一侧以显示第三个容器视图 (C)。我遇到的问题是,当我从 B 移动到 C 然后将手机侧翻时 B 和 C 重叠,如果我按下移动按钮,它们不会保留它们的位置并且位置突然重叠,因为它们都是重叠他们都走出了屏幕。我添加了 3 个显示行为的屏幕截图:
https://imgur.com/a/wluCkpT
到目前为止,这是我在 viewDidLoad 上的设置,我调用了 resetPositions():
class HomeVC: UIViewController{
var isMainContainer: Bool = true
func resetPositions() {
if isMainContainer {
if UIApplication.shared.statusBarOrientation.isLandscape {
containerViewBot.center.y = containedView.center.y
containerViewBotBack.center.y = containedView.center.y - offset
} else {
containerViewBot.center.x = self.view.center.x
containerViewBotBack.center.x = self.view.center.x - offset
}
} else {
if UIApplication.shared.statusBarOrientation.isLandscape {
containerViewBotBack.center.y = containedView.center.y
containerViewBot.center.y = containedView.center.y + offset
} else {
containerViewBotBack.center.x = self.view.center.x
containerViewBot.center.x = self.view.center.x + offset
}
}
}
现在,当我按下按钮从一个视图“移动”到另一个视图时,“isMainContainer”会发生变化:
func getMore(from: String) {
//from is the sender
if from == "homeResults" {
containerViewBotBack.isHidden = false
goToCommute()
} else {
//containerViewBotBack.isHidden = true
containerViewBot.isHidden = false
goToResults()
}
}
最后是 goToCommute 和 goToResults 方法:
func goToCommute() {
let duration = 0.5
if UIApplication.shared.statusBarOrientation.isLandscape {
UIView.animate(withDuration: duration, animations: {
self.moveUp(view: self.containerViewBot)
self.moveUp(view: self.containerViewBotBack)
}) { (_) in
self.containerViewBotBack.isHidden = false
self.containerViewBot.isHidden = true
}
} else {
UIView.animate(withDuration: duration, animations: {
self.moveRight(view: self.containerViewBot)
self.moveRight(view: self.containerViewBotBack)
}) { (_) in
self.containerViewBotBack.isHidden = false
self.containerViewBot.isHidden = true
}
}
isMainContainer = false
}
func goToResults() {
let duration = 0.5
if UIApplication.shared.statusBarOrientation.isLandscape {
UIView.animate(withDuration: duration, animations: {
self.moveDown(view: self.containerViewBot)
self.moveDown(view: self.containerViewBotBack)
}) { (_) in
self.containerViewBot.isHidden = false
self.containerViewBotBack.isHidden = true
}
} else {
UIView.animate(withDuration: duration, animations: {
self.moveLeft(view: self.containerViewBot)
self.moveLeft(view: self.containerViewBotBack)
}) { (_) in
self.containerViewBot.isHidden = false
self.containerViewBotBack.isHidden = true
}
}
isMainContainer = true
}
我在覆盖 viewWillTransition(...) 时调用“resetPositions()”命令
【问题讨论】:
标签: ios swift uicontainerview