【问题标题】:Repositioning views on viewWillTransition在 viewWillTransition 上重新定位视图
【发布时间】: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


    【解决方案1】:

    您可以通过以下方式检测设备旋转并重新定位您的项目。

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        NotificationCenter.default.addObserver(self, selector: #selector(deviceRotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    }
    
    func deviceRotated(){
        if UIDeviceOrientationIsLandscape(UIDevice.current.orientation) {
            print("Landscape")
            // Resize other things
        }
        if UIDeviceOrientationIsPortrait(UIDevice.current.orientation) {
            print("Portrait")
            // Resize other things
        }
    }
    

    【讨论】:

    • 我已经在 func override ViewWillTransition 中检测到了旋转。
    • 那么您的具体问题是什么?
    • 旋转发生时的重新定位会覆盖容器视图,而不是通过偏移保持它们关闭
    猜你喜欢
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多