Adding SearchBar to CollectionView - ios

I've created a CollectionView (as a CollectionViewController in IB) and it occurred to me that I should have a search function.
I want to add a SearchBar above my CollectionView in Storyboard, however it physically will not allow me and keeps sliding below which obviously won't work.
Is there a way to get around this, or do I need to rebuild my CollectionViewController from scratch with SearchBar at the top?
Can I add the SearchBar programmatically to the existing CollectionViewController?
See picture for reference:

I think the easiest way is to copy your collectionview to UIViewController & setting it to be delegate & datasource.
This way will allow you to add a searchbar above collectionview.
you can further detect searchbar text change & apply changes to collection view data.

Related

TableView and CollectionView in the same ViewController with Swift

I'm building my application with Swift and I want to set up TableView and CollectionView in the same ViewController.
The only answer I founded is to setup my CollectionView in the TableViewCell of the TableView.
I did it but it scrolls separately.
The solution I found is to disable CollectionView scrolling and then configure CollectionView's height constraint programmatically by summing heights of the cells. It isn't perfect at all because cells become non reusable (250+).
I'm in desperation because I have seen this setup in many other apps and I think that answer is so simple but I can not find it anywhere.
Can anyone please help me? Thank you a lot!
Another option is to set it up as a single collection view with two sections, one that looks and lays out as a tableview and the other that has the more collection view appearance.
Or set it up as a single table view with two sections, one of which displays two side-by-side images per line (this is the old school way of creating this layout.)
Lots of ways to go about it, depending on what you want to play with and what you want to learn :)
You can put a UITableView and UICollectionView inside a UIViewController, and implement UITableViewDelegate, UITableViewDataSource and UICollectionViewDataSource.
Keys:
Use AutoLayout to make sure tableView has only two rows, and
collectionView takes the rest of area.
Make sure tableView.scrollEnabled = false
Here is a sample, https://www.dropbox.com/s/v6yc40udfk8mqx5/FlexTableCellHeight.zip?dl=0
Set up a blank ViewController. Then place two ContainmentViewControllers inside of the ViewController. Size them however you want based on how much screen you want to the be tableview and how much you want to be collectionview. Then create segues from each containmentViewController to your tableviewcontroller and collectionviewcontroller using embed. This will allow both to be on screen at once and scroll separately.
If you do not need to use a TableViewController and can get away with just a tableview you can also just place one containmentviewcontroller below your tableview and create the embed segue to your collectionviewcontroller.

keep keyboard from hiding UITextField at bottom of UITableView

According to the answer at UITableView, make footer stay at bottom of screen?, which I verify:
In order to have a footer that stays put at the bottom of the screen,
and doesn't scroll with the table, then you can't use a table view
footer. You can't even use a UITableViewController, you'll need to
implement your view controller as a UIViewController. Then you add
your own table view as a subview. You'll also need to add your footer
as a subview of the view controller's view, not the table view. Make
sure you size the table view so its bottom is at the top of the footer
view.
but the problem is that my UITextField, inside my footer, is being hidden by the keyboard when user tries to type. So how do I keep the keyboard from hiding the UITextField? Throughout the app, I have been using TPKeyboardAvoiding. But in this case, where a UIScrollView/TPKeyboardAvoidingScrollView contains a UITableView and a UIView in vertical order, it does not work. I generally like TPKeyboardAvoiding because it’s so quick and easy. Any ideas how I might fix this issue?
The instruction that you quote is telling you to use a UIViewController, add a UITableView as a subview, then add a UITextField as a subview of the UIViewController NOT as a subview of the UITableView.
Let me draw it out for you:
Step 1:
Add a UITableView to your view controller
Step 2:
Add your UITextField subview (you can embed the UITextField in a UIView container if you like) as a subview of your UIViewController's view NOT your UITableView footer.
Step 3:
If depending on if your using frames or autolayout, you adjust the frame or autolayout constraint constant values for your UITextField subview when user tap on the text field.
Step 4:
Finally, the keyboard appears and the UITextField doesn't get obscured by the keyboard.
Just make sure your UITableView doesn't initialize it's own automatic scrolling for keyboard appearing. If you use UITableViewController then just subclass it and make sure you don't call it's viewWillAppear:. I didn't investigate which exactly method you should block without using UITableViewController, so, you should find out this yourself or somebody else would help.
I did similar in my own project where I wanted to have own scrolling of table view for keyboard appearing.

How would I create an "About" page like this for my app? How do I have a UITableView (grouped) under a UIView?

Take this about page for Things:
I'm having trouble creating something similar. I just want a UITableView under a UIView with a UIImageView and a UILabel in it.
If I use a UIViewController and so I can position the UITableView downward, I get this error: "Static table views are only valid when embedded in UITableViewController instances."
If I use a UITableViewController with a grouped style and use contentInset on self.tableView to move it down ([self.tableView setContentInset:UIEdgeInsetsMake(150,0,0,0)];) I can't figure out how to place a view above it. If I try to attach anything to self.view it crashes (obviously). Same happens if I attach anything to self.tableView.
I then tried making the UIView the header of my UITableView section (I only need one section) but I can't get it to move up enough. It just sits inside the UITableView almost.
How do I have a UITableView (grouped style) exist with a UIView above it?
This can be achieved easily using the tableHeaderView property of UITableView. If you are using Interface Builder (which it looks like you are), then you can just drag a UIView above the table view and it will be set as the table's header view. All you need is a UITableViewController; no need for UIViewController and manually laying it out.
That's because the view probably isn't placed on top of the table but rather within the table's section 0 header. Or, even more likely, the view in question is just a regular UITableViewCell with a 0 alpha background.
Either of these options would allow the top view to be scrolled out of frame as the user scrolls under every condition.
I recommend [MDAboutController] (https://github.com/mochidev/MDAboutController)
It's easy to integrate and you don't have to waste any time configuring the UITableView.

Adding subviews on a UITableView

I'm working on a project were I use mostly UITableViews. The user enters data and the App gives the result of a calculation. I have two buttons: "Calculate" and "Reset". I want to add a subview containing these two buttons and have them displayed at the bottom of the screen. By Storyboards, I can add the subview, but it sticks to the button of the UITableView, not the screen.
Any idea on how to accomplish that? Should I mess with the constrains between the subview with the buttons and self.tableview.superview ?
Any help is appreciated!
You can use a standard UIViewController instead of a UITableViewController, and add a UITableView to your view controller. You can then place subviews at will.
Do not forget to connect datasource and delegate of your tableview.

UITableView won't scroll with a UISearchBar

This should be a fairly general question, but I have a view in which I placed a UITableView and implemented it properly. It works well and it scrolls. But I want to add a UISearchBar attached to it so that it will scroll along with the table. But when I add the search bar onto the tableview it shows up, but the tableview will not scroll anymore. I don't actually need any input into the search bar, and I disabled user interaction. Is there any way to add the search bar (no need to implement it) and have it scroll?
(I will rate up anyone who answers!)
Thanks
The UISearchBar should be the UITableView's header view.
Had similar issue even when IB had the correct bouncing properties enabled.
In your viewDidLoad, set self.tableView.alwaysBounceVertical = YES;.

Resources