根据 Swift 中的条件动态更改屏幕内容的最佳方法是什么?

What's the best way to dynamically change screen content based on condition in Swift?

我将内容以特定模式存储在数据库中:

我需要按特定顺序在屏幕上显示此信息。用户点击屏幕的右侧或左侧以导航到下一个内容页面。这类似于 Instagram 故事,但文本可以按不同的顺序出现。

故事板似乎对变化的内容并不十分灵活 orders/type。将模式识别为某种模板形式然后切换到该形式的最佳方法是什么?新模板应该是 .xib、故事板还是编码?

我注意到像 Medium 或博客网站这样的应用程序可以根据所写的内容以任意顺序放置图像或文本。这怎么可能?

我在这些情况下使用 UIPageViewController。
UIPageViewController 是一个容器视图控制器,用于管理内容页面之间的导航,其中 a child view controller 管理每个页面。

您可以创建多个要构造的视图,并在子视图中显示它们。

我将向您展示示例代码。

pageController = UIPageViewController.init(transitionStyle: UIPageViewController.TransitionStyle.scroll, navigationOrientation: UIPageViewController.NavigationOrientation.horizontal, options: nil)
        
        pageController.view.backgroundColor = UIColor.clear
        pageController.delegate = self
        pageController.dataSource = self
        
        for svScroll in pageController.view.subviews as! [UIScrollView] {
            svScroll.delegate = self
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            
            self.pageController.view.frame = CGRect(x: 0, y: self.viewLine.frame.maxY + self.view.safeAreaInsets.top, width: self.view.frame.size.width, height: self.view.frame.size.height-(self.viewLine.frame.maxY + self.view.safeAreaInsets.top))
        }
        
        tab1VC = self.storyboard?.instantiateViewController(withIdentifier: "MySaveListVC") as? MySaveListVC
        tab2VC = self.storyboard?.instantiateViewController(withIdentifier: "MyReviewListVC") as? MyReviewListVC
        
        
        arrVC = [tab1VC, tab2VC]
        
        pageController.setViewControllers([tab1VC], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)
        
        self.addChild(pageController)
        self.view.addSubview(pageController.view)
        pageController.didMove(toParent: self)

其中 tab1 和 tab2 是子视图。

您可以使用按钮或滑动手势移动视图。