How to insert custom view on the bottom of tableview? - ios

In the case of loss network, I'd like to insert a custom view on the bottom of table view but not overlap the table view. This custom view still appear even the user leave their finger on screen and only disappear after a period of time or network come backs. It likes the behaviour of Facebook app. How can I do that?

//Give size of view as u want
let customView = UIView(frame: CGRectMake(0, 0, 200, 50))
//add this view in footer section of tableview
youTableview.tableFooterView = customView

You can do set footer as per below screenshot.

Related

UIView detaches from bottom of UICollectionView on scroll

I have a UICollectionView that allows a user to enter reps via a UIView containing a picker. The picker container attaches to the bottom of the view as long as the UICollectionView doesn't need to scroll. As more cells are added and the view must scroll, my picker container no longer attaches to the bottom of my view. I have attached a screenshot of my issue and code showing how I have attached my pickerContainer to the bottom of my collectionView.
let pickerContainer = UIView(frame: CGRect(x: 0, y: self.collectionView.frame.height - pickerContainerHeight, width: self.collectionView.frame.width, height: pickerContainerHeight))
<....some other code that i dont think matters....>
collectionView.addSubview(pickerContainer)
Don’t add it to the collection view as it will be added to scrollable area.
You can add it to the superview of collection view and put it above collection if you using storyboard. Or move in front using pickerContainer.superview?.bringSubviewToFront(view: pickerContainer) in case you interested in only in programmatically version.

Make cell stick to the bottom of a tableview

Hello I am trying to figure out how can you stick the last cell/section on a tableview to the bottom screen. This is the screenshot.
I am trying to put the create account at the bottom. I've tried using the tableview footer but this is what I get.
How would I be able to put that red view at the very bottom. I know I can just use a view controller and drag a table view controller and place the button at the bottom. However I would like to learn how to do this using the tableview controller. Would really appreciate any help :)
This is the table view footer code I am using in the viewDidLoad. Which creates the red view at the bottom of the create account.
let footerView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 40))
footerView.backgroundColor = .red
tableView.tableFooterView = footerView
Okay, I see what the issue is.
When you assign the UITableView.tableFooterView it puts it at the end of all the cells. It's not fixed to the bottom of the table.
Because UITableViewController does not have a parent view outside of the tableview, there is no way to add a view to the tableview that will not also cover up additional cells.
For example, you could programmatically create a UIView, add constraints, and then add it as a subview of the tableview - this would likely have undefined behavior if the table view ever changes the number of rows in it.
Even if it worked, it would likely cover up cells as you scrolled.
Unless there is some external constraint, I recommend you use the correct tool for the job - make a UIViewController and put a UITableView and your red view underneath it.
Add your button on storyboard under of your tableview. in your case there is no reason to have it in a tableview cell
You may use a little trick: as you know, header stick to the top of the table view. What you can do is mirror the table view and its contents horizontally by applying scale transform, like so
tableView.transform = CGAffineTransform(scaleX: 1, y: -1)
also, in your cellForRowAt
cell.transform = CGAffineTransform(scaleX: 1, y: -1)
and in viewForHeaderInSection
headerView.transform = CGAffineTransform(scaleX: 1, y: -1)
Just make your Create Button a section header and apply transforms.
Alternatively, you can simply add that button as a subview to your table view on the bottom, and provide an empty view as section footer view, with height equal to your button's height.

Scrollable UITableView as an overlay

I'm trying to replicate the following GIF from Postmates checkout - a scrollable UITableView positioned on top of a MapView. This tableview can be scrolled, with the normal bounce effect if I go too far down or up.
Currently, I have the MapView and UITableView added as sibling views to my ViewController's view. I have adjusted the frame of the table view to move it down.
CGRect rect = CGRectMake(
0.f,
200.f,
self.view.bounds.size.width,
self.view.bounds.size.height - self.navigationController.navigationBar.bounds.size.height - 200.f
);
The two main issue's I'm having are:
I cannot figure out how to drag the entire tableview down when pulling down. E.g. the grey (my tableView.backgroundColor) sticks when I scroll down. If however, I make that background clear, then when I drag up, you see the map emerging from behind the view.
My cells keep disappearing when I scroll up. I have clipsToBounds = false, and I'm not actually dequeuing cells, just creating them in my cellForRow method, but they still disappear.
I feel like this should be a straightforward layout, but I'm missing something!
I've tried adjusting the contentInset of the table view, but then the scrollbar does not align with the cells as it does in the gif and does not look nice.
We needed similar effect in our app, along with parallax in the underlying view(map here/ we had a photos gallery).
I assume you want something like shown on this blogs video here
I wrote a small blog on how to achieve this. You can find this here
Basically its just a play of contentInsets and contentOffset properties of UITableView
If this does not suits you, here my suggestion in your two main points.
Controller's View has subviews
Map View (fills complete super view)
UITableView (fills complete super view) and in code set content insets.top = kHeightOfVisibleMap
The solution was simpler than I thought, no autolayout or crazy weird tricks required.
Add a full screen table view to your controller, and insert a full screen map view behind it.
self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
[self.view insertSubview:self.mapView belowSubview:self.tableView];
Add a dummy view with the background color of your table view with a height of around 200 pixels, and a full width, into the tableFooterView on your table view.
CGFloat footerHeight = 200.0;
UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, footerHeight)];
dummyView.backgroundColor = [UIColor whiteColor];
self.tableView.tableFooterView = dummyView;
Set the content offset of the table view to be the inverse of that footer view's height:
CGFloat footerHeight = dummyFooterView.bounds.height;
self.tableView.contentOffset = CGPointMake(0, -footerHeight);
Set the content inset of your table view to offset the footer view.
self.tableView.contentInset = UIEdgeInsetsMake(footerHeight, 0, -footerHeight, 0)
Adjust the scrollbar position, again, based on the footer's height.
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(footerHeight, 0, 0, 0);
This will push down the tableview, ensure the scroll bars match the table view, but allow it to bounce 'up' above its initial position and will ensure the background does not peek through the bottom of the tableview.

UItableview rows overlaps textfield

I have view controller which has a tableview and below the table view there is a textfield(which is not part of tableview cell) and a button(just like whatsapp chat window)
What I see is, when I start adding objects to the table, it grows up to the text field and grows beyond and below the text field at the bottom.
There are a few different ways to go about it,
In the IB, Make sure that your tableview comes before your textfield and button, The top most view is the farthest one.
You can use UIView methods such as
sendSubviewToBack: (Send tableview to back)
bringSubviewToFront: (Bring textfield and button to front)
Have you tried setting frames to each of these elements?
CGFloat tableViewHeight = 400;
self.tableView setFrame:CGRectMake(0, 0, CGRectGetWidth(self.view), tableViewHeight);
I'd place the textField and button as subviews to a view - self.textFieldElementsView and set the view's frame under the tableView:
self.textFieldElementsView.frame = CGRectMake(0, CGRectGetMinX(self.tableView.frame) + tableViewHeight, CGRectGetWidth(self.view), CGRectGetHeight(self.view)-tableViewHeight);

How to add a search bar at the bottom of a table view in Xcode?

I am developing an iOS application, and I want to add a search button that initiates a search of a table view. When searching, the search bar should not be scrolled with the table cells.
To make a search bar (or any view really) "stick" to the top or bottom of a UITableView there are two approaches:
Adjust the frame of the table to be the view height minus the height of your bar, and then set the bar's frame to {0, 0, CGRectGetWidth(self.tableView.frame), CGRectGetHeight(self.tableView.frame)}, which would position it statically at the bottom of the view. If you are using a stock UITableViewController, you'll need to do some additional work because self.view and self.tableView both point to the same object. You'll need to set a new UIView to self.view which will then be the container for the table view and your search bar view.
Add the bar as a subview of table, then implement UIScrollViewDelegate in your controller and use scrollViewDidScroll: (which fires whenever the user scrolls the view) to update the bar's position. The new position would be something like:
CGRect adjustedFrame = self.searchBarView.frame;
adjustedFrame.origin.y = self.tableView.contentOffset.y + CGRectGetHeight(self.tableView.frame) - CGRectGetHeight(self.searchBarView.frame);
self.searchBarView.frame = adjustedFrame;

Resources