I am very new to Swift, object-oriented programming, and interface building (I only have experience with some MATLAB programming), and I have been struggling for about a week to come up with a working interface in the IB. I have tried lots of permutations of View Controllers the Container View with and without a UIButton connected to a segue and with and without a Navigation Controller, and I am out of ideas about what to try at this point (the Stack Overflow pages I've looked at ended up created more bugs with what I wanted to do).
Ultimately, I want the user to be able to use UIButton to navigate between six UIViewControllers that are inside a Container View in a View Controller (sort of like a manually-operated slide show between the views). I have tried UITabBarController and UINavigationController, but I don't want the tab or navigation bars.
I apologize in advance for not providing any code, but I literally learned Swift a month ago, and am sort of learning as I go along. I have uploaded one of my attempts in the IB though (two out of six UIViewControllers are shown because I haven't put all of them in yet due to the bugs). In the attached image, the second view is placed on top of the first rather than just in the Container View. the image is on the URL since I cannot attach it to this postThank you for any help that you can provide.
How I would do it is pretty straight forward. Create a collection view with so many number of cells as you need Views. Lets say 5. Each cell has its own UIView with an embedded ViewController. You do that 5 times for each view. That should be the difficult part.
The next thing would be to create the button I guess you have 2 buttons one back < and one forward >. Then simple logic on button press and in the collection view you just increment to the next page using this:
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
That should do the trick.
An example on how to change the cell size
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
// your code here
}
For different views for each cell you create a cellId1, cellId2, cellId3 etc. And register a new cell for each cell id.
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
This would be how to set a different cell for the views
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
return cell
}
This should be really enough to get you going.