如何在没有导航控制器的情况下在相同 viewController 的情况下在子视图之间转换?

How To Transition Between Subviews While Being In The Same viewController without Navigation Controller?

我是 iOS 开发新手。我想在 UIKit 的帮助下编写一个页面,我可以在子视图之间切换(通过单击单选按钮),动画效果不错(如本视频所示: https://www.youtube.com/watch?v=ZXXWkieFzN8)。但是,我不知道如何做这样的事情。我正在尝试在我的 reset passowrd 页面中实现此逻辑,用户可以 select 重置密码的最佳选择。

在标准 SDK 中有 UIPageViewController https://developer.apple.com/documentation/uikit/uipageviewcontroller

但如果您需要自定义动画/视图,则更容易:

  1. 创建 holder UIView,您将在其中更改内容

  2. 为您的自定义内容创建不同的 UIView(或 UIViewController)

  3. 当您需要更改内容时 - 您从 holder 中删除以前的 UIView 并插入一个新的 UIView(从而交换它们)。

我尝试使用 UIViewControllers 而不是 UIView 的内容,因为它更可重用。

给你举个例子

class DynamicViewManager {
    
    private weak var contentView: UIView?
    private(set) weak var currentShownVC: UIViewController?
    
    
    init(contentView: UIView) {
   
        self.contentView = contentView
    }
    
    
    func show(viewController: UIViewController) {
        
        guard let contentView = self.contentView else {
            
            return
        }
        
        viewController.view.translatesAutoresizingMaskIntoConstraints = false
        
        self.removePresentedVC()
        self.currentShownVC = viewController
        
        contentView.addSubview(viewController.view)
        contentView.sendSubviewToBack(viewController.view)
        NSLayoutConstraint.activate(self.pinningConstraints(view: viewController.view, to: contentView))
    }
    
    
    @discardableResult
    func removePresentedVC() -> UIViewController? {
        
        let cached = self.currentShownVC
        self.currentShownVC?.view.removeFromSuperview()
        self.currentShownVC = nil
        return cached
    }

    private func pinningConstraints(view: UIView, to anotherView: UIView) -> [NSLayoutConstraint] {
    
      return [
            view.leadingAnchor.constraint(equalTo: anotherView.safeAreaLayoutGuide.leadingAnchor),
            view.trailingAnchor.constraint(equalTo: anotherView.safeAreaLayoutGuide.trailingAnchor),
            view.topAnchor.constraint(equalTo: anotherView.safeAreaLayoutGuide.topAnchor),
            view.bottomAnchor.constraint(equalTo: anotherView.safeAreaLayoutGuide.bottomAnchor)
        ]
    }
}

在你的情况下它看起来像这样

let manager = DynamicViewManager(contentView: self.contentView)
let left = LeftViewController()
let right = RightViewController()
.onLeftClick {
    manager.show(left) 
    self.changeHeight(for: contentView, toPercentage: 15)
}

.onRightClick {
    manager.show(right)
    self.changeHeight(for: contentView, toPercentage: 30)
}

您还可以调整 ContentView 的高度,让您的所有内容都在下方(重置密码也会移至顶部)

您通过停用先前的约束并激活新的约束来更改高度(以使 self.changeHeight 起作用)