How to add UIButton to UICollectionView which will scroll with its cell? - ios

I was trying to build home base application which mean there are many icon at root view and it will navigate to appropriate view. So, I will use collection view for it. But, the problem is when I add button to the collection view, the button didn't scroll with collection view, when I scroll collection view up. Here is my simulator build:
As you can see,the button "Click here to login" didn't scroll up with collection view. So, is there any way that I can add button to center of collection view which will scroll with collection view?
Here is my storyboard :
What am I doing wrong?

When you take a closer look at your view hierarchy in Interface Builder you'll notice that the button you added isn't actually a subview of your collection view. I added a red line to indicate which objects belong to the collection view.
Your login button is on the same level as your collection view in the view hierarchy. Both are subviews of your view controller's root view. So no wonder the button doesn't scroll with the collection view.
However, you cannot simply add an arbitrary view (or button) to the collection view itself in Interface Builder. You'll need to add a Collection Reusable View first as your collection view header and then add your button to it.
Your storyboard scene in Interface Builder will then look like this:
Please note that you'll also need to add some code to your collection view data source (which will probably be the same as your collection view controller) in order to make this work.

Related

How to constrain static table view controller cells to Safe Area in Swift/iOS

I have a table view controller with static cells. I’m running on iPhone X and when scrolling, the cells appear to be behind the navigation bar and unsafe area. What is preferred would be a solid orange color in the unsafe area instead of the image presented below. Is this possible? I can’t move to a table view inside of a view controller, which I would then constrain to the safe area, because my cells are static.
You need to use a Container View, so add a View Controller in your storyboard and put a Container View in it-by adding the container view it will automatically create a segue and a view controller delete both.
Now from your Container View create a segue to the Table View Controller and choose Embed.
Last step is to set the constraints for your Container View, the most important for your issue is the top one, set it 0 to the safe area.

How can you make UITableView a subview of UIView retrospectively?

I have a view controller (subclass of UIViewController, not UITableViewController), with a table view as a subview. I now want to put another view on that screen, but can't resize the table view to not take up the entire screen, or drag in a view above that. It seems that the view can only contain a single table view. I know it's possible to have a table view and other views within the same screen, so what am i missing here?
Take a look at your view hierarchy on left - your view is subview of tableView. And you need to have something like
ViewController.view
|--> view
|--> tableView
And you have
ViewController.tableView
|--> view
Edit
What should you do
Copy (Cmd-C) your Table view. Than delete it
Drag View to your View Controller
Choose View and paste Table view to it (Cmd-V)
Drag View from Table view upwards to root view of Controller
Now, you can position your view and table view however you want

Container View or UIViewController for tableView with SubView

I have a UIViewController in storyboard that will be a sliding up menu. I want to place it in a UITableView. If I place it as subview of UITableView and prevent it from scrolling, it gets the look I want, with one problem, the section headers of tableview scroll above subview.
Now, in order to prevent this behavior, I could reduce tableview frame, but it doesn't work because my subview is inside that same frame, right?
So , if that is true, what is the best way to achieve this? Place both screens inside a third UIViewController?
Don't try to put other views inside a table view. Bad news.
If your table view is managed by a UITableViewController then that's really all you can have in the table view (unless your views are inside cells, or in headers or footers.)
If you ARE using table view controllers to manage your table view(s), I suggest you create a container view on the view controller that needs to contain the table view, then control-drag an embed segue to the table view controller. That way the table view controller becomes a child view controller of the main VC, and you can put other view elements on the screen to your heart's content.

Embed UITableView in Scrollview

I know Apple don't recommend to Embed UITableView in UIScrollview.
So this is what I am trying to do:
I have a registration form with fields embedded in UIScrollview,
for some fields I create a drop menu by presenting UITableView, the problem is when the UITableView appears with my object the didSelectRowAtIndexPath not responding.
If there is another way it I will happy to hear, if not how can I fix it in the Current situation , thanks.
Don't add the table as a subview of the scroll view, add it as a subview of the superview of the scroll view (and, consider adding it into a container view which detects touches outside the table to dismiss it from the screen without any selection being made).
If your 'root' view for the controller is the scroll view, change your view hierarchy to use a plain view as the root and have the scroll view as a subview of that.
You may also want to use bringSubviewToFront: to ensure that the presentation is correct.

Embedding a UIContainerView as a portion of screen in a UIViewController

I have a screen in my storyboard where I would like to embed a container view into a portion of the screen. I can't figure out if I should add a new containerview or view from the object library and then drag in a collection view controller or collection view (or tackle it a different way?). I've tried all combinations but just end up with the embedded view being shown black even if I drag collection view cells and set a background colour to test it. Once I can get this working I plan to populate the cells from an external database.
Any suggestions please? I'm doing this using storyboard as far as possible.
Thank you.
After you drag a container view into your view, you automatically get an embedded view controller, whose size is matched to the container view's size. You should delete that controller, and drag in a UICollectionViewController. Control drag from the container view to this controller and choose embed when you let go -- this will resize the collection view controller. You can give the collection view a background color, so you will see where it is, but giving the cells a background color won't show up until you implement the methods in the controller to populate those cells.

Resources