UIButton for navigation between six UIViewControllers in a Container View - swift

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.

Related

How to add Activity indicator to end of UICollectionView

Hi I have been trying to add an Activity Indicator, or a Loading spinner, to the bottom of my collection view. I have searched online on how to do so but there is not a clear way to do so.
I do not want to use a cell for the indicator itself because my cells a pretty big. (2 cells take up the whole screen.) So I would just like to have a small loading spinner at the bottom for when users scroll down.
Through the research that I have done I have seen that some people use this function below and a separate class to build the spinner.
private func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) -> UICollectionReusableView{}
But I do not know how to use it. I already created a class called LoadingViewController() but in all honesty don't know if it is event he right type. Currenlty I have it set as a UICollectionReusableView
For a little more background, I already added the ability paginate data. So when I scroll down the collection view, new cells are loaded. Now the problem is I want to show a little spinner at the bottom when the data is loading because if not the app stutters and looks like it is broken.
Or is there a better way to show load items in a collection view that does not cause the scroll to freeze?
Please let me know if you have any questions regarding my situation I'll try to answer them to the best of my ability.

How to implement different UI screens to my playground

Im trying to include a tap to open screen on my playground that then allows users to see my chart. I want them to first tap through 3 or 4 screens with different information before loading to my main page.
Ive tried searching up tutorials (Im new at this), but cant find any that help me.
So this is the code to show my table view and need help implementing the tap screens
class HOCMasterViewController: UITableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return reasons.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell!
cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
My table appears, just need to add the UI for the tap screens!
First of all the information screens that you need users to tap through. How are they being displayed? Are you using simple UIViews? Kindly give us some info on that? If you are using different UIViews on which you are showing information, then I would simply hide the previous UIView/ remove it from superView and add the next UIView and so on!
So your playground loads and when you tap on the screen by using a UITapGestureRecognizer and in the selector function you count the taps and according to each tap, you can hide or show the UIViews / or animate the constraints / frames of each UIView. Then after 3 taps you can show the TableViewController.
Kindly provide us with the information screen code.

Swift UITableViewCell subview do not register tap

Background: I'm making a calendar and for that I'm combining a UITableView and a UICollectionViews. The UITableView is holding month cells. Each month UITableViewCell is holding a UICollectionView with UICollectionViewCell days.
Problem: The UICollectionViewCells aren't clickable.
I can fix this by calling self.bringSubviewToFront(self.collectionView) in the month UITableViewCell. However this makes the UITableView not scroll because the collectionView is now top view.
So, I can either choose between be able to scroll the UITableView or be able to click the UICollectionView.
What I tried: I tried to capture the click before it happens and bring the collectionView to front and then put it back again. Not a good solution though:
//In UITableViewCell class
//UIGestureRecognizerDelegate
//Bring collectionView to front before tap
override func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
self.bringSubviewToFront(self.collectionView)
return true
}
//UIScrollViewDelegate
//Bring collectionView to back again
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
self.sendSubviewToBack(self.collectionView)
//Do stuff
}
Question: How can I achieve to both be able to scroll and have the UICollectionView clickable without bringing back and forward the view between taps? Is there a way to have the UICollectionView in front and still make the UITableView scrollable?
Nested UIScrollView with same scroll directions is against Apple's Human Interface Guidelines and your app might even get rejected for this so try disabling the scroll of your UICollectionView.
Hope this helps.

How to focus a UICollectionViewCell in UITableViewCell in tvOS?

Description:
I have a list of UITableViewCells that have each have a UICollectionView with X amount of UICollectionViewCells (see image below for more clarity)
The idea behind this is to be able to implement sections with a header on top and a horizontal scrolling UICollectionView
Everything is working great, except when I try to focus the UICollectionViewCells (aka: CategoryCell). The UITableViewCell gets focused and highlights all the content making it impossible to focus any of the UICollectionViewCells.
What I've tried:
According to other posts the fix for this would be deactivate User Interaction and/or set the selection style to none on the UITableViewCell:
cell.selectionStyle = UITableViewCellSelectionStyle.None
and to enable User Interaction on the UICollectionViewCell instead.
Here are posts that talk about it:
UITableViewCell with UITextField losing the ability to select UITableView row?
How can I disable the UITableView selection highlighting?
I've attempted to implemen these solutions without success.
Question:
What step did I miss? Does the fix not work on tvOS? How do I focus my UICollectionViewCell that is in a UICollectionView that is in a UITableViewCell?
More:
Image showing my UITableViewController and all it's contents from my StoryBoard.
The problem is most likely that your table view cells are returning true to canBecomeFocused. You can disable that by either implementing the UITableViewDelegate method tableView(_, canFocusRowAt:) to return false, or by subclassing UITableViewCell and returning false from canBecomeFocused.
The focus engine will try to focus on the top-most view that returns true from canBecomeFocused, which is why it can't find your collection view cells: they're "hidden" inside the focusable table view cells. If you disable focus on the table view cells, then the focus engine will be able to find your collection view cells.
override func tableView(tableView: UITableView, canFocusRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}

Segmented controller

UIView,SegmentedController & UIContainerView in a ScrollView or something similar?
In my storyboard I have a VC containing a UIView at the top, segmented controller in the middle & 2 ContainerViewControllers at the bottom with embeded segue's to new VC's.
In the new VC's I have a TableView & a CollectionView (For example, the Instagram profile).
My issue is that I need the SegmentedController and UIView to scroll with the ContainerView(TableView/CollectionView), at the moment only the ContainerView part scrolls and the parts above that are fixed.
Now I'm guessing I probably need to put everything into a UIScrollView, so I tried that & placed all the costraints correctly. But when It came to configuring it in the Swift file, I only really now how to set the height of the scroll but oviously the height I need can vary and isn't a fixed height!
If anyone can help me here that would be fantastic, or maybe point me towards a similar question already asked here? I have already looked, but did not find anything unfortunately!
Here's an image below explaining basically what I'm after, if above I wasn't very clear..
here is an example you can see: https://github.com/Jackksun/ScrollIssue
the image is here, as I dont have enough reputation I cannot place it in the post!
You can use only UITableViewController. You will define three custom cells and in cellForRowAtIndexPath ask for the Index number, if it is 0 you will set the elements in that cell accordingly
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: TableViewCell!
// stuff in UIView
if indexPath.row == 0 { set the cell here}
// for UISegmentedController
if indexPath.row == 1 { set the cell here}
// cells that will repeat
if indexPath.row >= 2 { set the cell here}
return cell
}
Update1
Define how many custom cells you will use in Attribute inspector for tableView, you will see them instantly in Storyboard
Update 3
Did you mean this? It is completely table view with 4 custom cells. I didnt use Collection view at all but I think it works as you wanted