swift - 用于在容器 View 中的六个 UIViewController 之间导航的 UIButton

标签 swift xcode uiviewcontroller uibutton container-view

我对 Swift、面向对象编程和界面构建还很陌生(我只有一些 MATLAB 编程经验),为了在 IB 中找到一个可用的界面,我已经苦苦挣扎了大约一个星期。我已经尝试了很多 View Controller 的排列,容器 View 有和没有连接到 segue 的 UIButton 以及有和没有导航 Controller ,我不知道在这一点上要尝试什么(堆栈溢出页面我已经看着最终我想做的事情产生了更多错误)。

最终,我希望用户能够使用 UIButton 在 View Controller 的容器 View 内的六个 UIViewController 之间导航(有点像在 View 之间手动操作的幻灯片)。我试过 UITabBarController 和 UINavigationController,但我不想要选项卡或导航栏。

对于没有提供任何代码,我提前表示歉意,但我实际上是在一个月前学习了 Swift,并且在学习过程中一直在学习。不过,我已经在 IB 中上传了我的尝试之一(显示了六个 UIViewController 中的两个,因为由于错误我还没有将它们全部放入)。在附图中,第二个 View 位于第一个 View 之上,而不仅仅是在容器 View 中。 the image is on the URL since I cannot attach it to this post感谢您提供的任何帮助。

最佳答案

我将如何做到这一点非常简单。根据需要创建一个包含多个单元格的 Collection View 。让我们说5。 每个单元格都有自己的 UIView 和一个嵌入式 ViewController。您为每个 View 执行 5 次。这应该是困难的部分。

下一步是创建按钮,我猜你有 2 个按钮,一个向后 < 一个向前 >。然后按下按钮和 Collection View 中的简单逻辑,您只需使用以下方法递增到下一页:

   [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];

这应该可以解决问题。

关于如何更改单元格大小的示例

func collectionView(_ collectionView: UICollectionView,
            layout collectionViewLayout: UICollectionViewLayout,
            sizeForItemAt indexPath: IndexPath) -> CGSize {
    // your code here
}

对于每个单元格的不同 View ,您创建一个 cellId1、cellId2、cellId3 等。并为每个单元格 ID 注册一个新单元格。

collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")

这就是如何为 View 设置不同的单元格

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) 返回单元格 }

这应该足以让您继续前进。

关于swift - 用于在容器 View 中的六个 UIViewController 之间导航的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45422633/

相关文章:

ios - TableView 单元格中的 MVVM

objective-c - 将不同方向的 UIViewController 推送到上一个

ios - MainViewController 没有收到来自 NSNotificationCenter 的通知

ios - 限制几个文本字段的长度

ios - 在 Swift 中获取 UIImageView 旋转值

c++ - 将不同组中的 C++ 文件包含到 Objective-C++ 测试中

ios - Swift:如何返回到我的 UIViewController 的同一实例

swift - 让 ViewController 仅在第一次启动时出现

ios - 这里是否存在更好的解决方案来处理 Swift 中的这个 api 问题?

swift - 获取 Parse 中与当前用户关联的所有结果