How add searchResultsTableView with constraints equal to the uitableview? - ios

I create simple view controller with table view and search display controller. When I added search display controller it create NSAutoResizingMaskConstraints for searchResultsTableView and take whole place in view controller. But I use autoLayout for this viewController and my tableView has a limited space.
Does anybody know how add constraints to a searchResultsTableView?
What I already tried:
I set searchResultsTableView.translatesAutoresizingMaskIntoConstraints = NO, but my UITableViewCell has autolayout too, and when I create my cell i see NSAutoresizingMaskLayoutConstraint error. This is weird because when i create this cell for tableView i dont see error. Cell created in StoryBoard like prototype for tableView.

I couldn't find a way to do this with constraints, but you can achieve the result you want by using a container view in the main controller set to whatever size you want your table views to be. The table view and the search results table view will both be the full size of this embedded controller. So, the storyboard would look like this:

Related

How to make the cells inside of a tableview begin lower down?

I have a tableview. Right now the first cell will begin right at the top of the tableview, but I want it to start some number below the top of the tableview so that I can addSubview() in that spot without covering content in the first cell.
Below you can see what I want to achieve.
The rectangle in the center represents the cells, the line at the top represents the top most of the tableview.
Short answer: You don't. The contents of a table view belong to the table view. You should not try to add subviews to a table view.
As #rickramirr says, you either need to position your table view lower on the screen, or use a header view that contains the contents you want.
(If you decide to position your table view lower down, need to either not manage your table view with a UITableViewController, or have a UITableViewController as a child view controller of the screen's main view controller. A UITableViewController is kind of dumb, and only knows how to manage a table view and nothing else.)
I think that maybe you should add a custom header to your table view or put another view above your table view. Here is Apple docs to customize your header: https://developer.apple.com/documentation/uikit/views_and_controls/table_views/adding_headers_and_footers_to_table_sections
All the views that you want to insert between one tableView cell and another must extend UITableViewCells too. You can create custom cells by defining a new xib file containing a UITableViewCell, whose content could be whatever view you want. Then, you must create a swift file that extends UITableViewCell associated with the xib file. Finally you need to record that cell into the table, either programmatically or by manually inserting it into the storyboard within the table. At that point you will decide its position using the UITableViewDelegate "cellForRowAt" method.

Xcode 9 swift 4 Dynamic reusable header for several viewController

I need 5 viewController all of them has the same header components. So I don't want to duplicate the same views and codes for each viewController. I would like to create a header and use the header for each viewController.
The problem is header will be filled by an object and this object has a list attribute. so i need to add this list attribute such a tableView.
What one view controller look like
I am trying to create an headerViewController and pass my object to it. And i will use this viewController in a container view for each 5 main view Controllers. But the height would be different so I can't define a height constraint to container view. in 5 view controller should use scroll view, because the total height will be higher than screen.
It is a little bit complicated because i try to solve this problem such a android developer :)
Assuming you want all items in tableView to be displayed all the time (no scrolling in tableView)
You should add height constraint to tableView or containerView
After loading/reloading the tableView with data set tableViewHeightConstraint OR containerViewHeightConstraint with content size height of tableView
tableViewHeightConstraint.constant = tableView.contentSize.height
OR
containerViewHeightConstraint.constant = tableView.contentSize.height
I have solved this issue creating a main view controller that contains a table view. And the common header is a type of tableviewcell. Then for each different body parts became another tableviewcell so according to type attribute of viewCOntroller, I am loading related tableviewCell.
I did this after #midhun-mp comment. Thanks to him.

How to create Layout with UIImageView and UICollectionView and collection view changes height on scroll?

I would like to create such a layout where on scroll the imageview hides and collection view takes whole screen. I tried this but it doesnt seem to work. I am getting the image view correctly but the collection view has gone dark completely on run. I have a content view(UIView) and scroll View underneath. Thank you
EDIT 1 :- The Cells are not getting displayed. But the collection view is displayed in the layout. How to solve this?
EDIT 2 :- Instead of using the collection view directly I used a container view and embedded my collection view in it and it Worked! However I am still trying to figure out how do i scroll the container view to top when i scroll, i.e. push the image view on the back of container view while it takes the full height of the screen.
Okay So I figured it out.
Wreck all of the above. (Though the container view solution is pretty good)
Build a collection View controller from scratch having 2 headers, one is the standard and the other one for the image view.
Make your view controller a delegate of the collection view flow layout delegate and implement function for referenceSizeForHeaderInSection
Do not forget to register for the xib yo have created for the image view header by using registerNib forSupplementaryViewOfKind function of collection view object.

How to add buttons and search bar into header at top of UITableViewController?

I have a search bar and some buttons in a view in my UITableViewController, but I don't know how to make that view the header? I've attached the referencing outlet to the UITableController. But currently the search bar and buttons scroll up and down with the tableview, but I want them to be the header so they stay fixed.
Here's a screen shot of what I currently have...
ok I ctrl/dragged from the view to my ViewController and created a IBOutlet, as you can see in this next image, and I added in the line to set it to be the header but it seems to give an error??
The easiest solution would be to move that view containing the search bar and buttons to being a subview of the view controller's view instead of inside the table view. Then apply your constraints so that it is pinned to the top and the table view is constrained to the bottom of the view.
If the list of elements you are displaying do not have more than one section, then you could implement the tableView:viewForHeaderInSection: delegate method. Create the headerview containing the searchbar in storyboard and return the instance. It will be always on top of the list.
Apple Documentaion

Removing a static UIView from a UITableView with Prototype Cells

I have a storyboard I'm maintaining that is a UITableViewController.
The UITableView is set to use Dynamic Prototypes and has 2 cells.
Above those two cells is a UIView inserted within the table view. That view contains a UIButton and another custom UIView. Both have constraints. The effect is that we have a table view with a button above it.
Now, in certain conditions, we want to remove that button and have the rest of the table view move up in the UI. I can remove that button -- by itself or by removing that parent UIView, but I cannot seem to to get the table to change the position of the prototype cells.
To hopefully make it clear, my document outline looks like this:
Table View
View (buttonContainerView mentioned below)
UIButton
View (child view mentioned below)
Constraints
Prototype Cell 1
Prototype Cell 2
I have tried several different approaches to remove the button. After reading that removing it from the superview will remove constraints as well, that's the approach I currently have in place.
[self.buttonContainerView removeFromSuperview];
I've tried removing the button, the child view, and the container view. I've also tried explicitly removing the constraints then removing the views. None of these seemed to work.
I've also tried the above with calls to setNeedsLayout and setNeedsUpdateConstraints on the table view, as well as setNeedsDisplay on the view.
I've also tried setting the row height to 0 for that first view, but it turns out it's not a UITableViewCell, so that doesn't work, either.
Of course, if I delete that container view from the storyboard, the prototype cells do appear in the UI exactly where they should.
Can you sense the flailing about?
What else could I be missing here to remove that top view effectively?
What I didn't realize is that the setup I have is a UITableView with a table header. To remove it and fix the spacing problem I was having I just needed to set self.tableView.tableHeaderView = nil in viewWillAppear:.

Resources