iOS: Place UIView on top of UITableView in fixed position
For people like me looking for a simple solution using Swift, these answers are kind of outdated. Here's what I did (assuming myCustomView was established somewhere else in the file):
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pixelsFromBottom = CGFloat(20)//or whatever the
let theHeight = self.tableView.frame.height + scrollView.contentOffset.y
myCustomView.frame = CGRect(x: 0, y: theHeight - pixelsFromBottom , width: self.view.frame.width, height: myCustomView.frame.height)
}
Make UIView fix position to the top of UITableViewController
Instead of using a UITableViewController, you should probably use a regular UIViewController and then add a UITableView and your custom UIView as subviews. UITableViewControllers aren't very flexible when it comes to adding subviews. They are meant to take up the entire screen.
For static table views you need to add a UITableViewController as a Container ViewController to another UIViewController. So you will have one main UIViewController that has your custom UIView nav bar and a Container View Controller that holds your UITableViewController.
UIView on top of UITableView
Well, since UITableView
is a subclass of UIScrollView
you could try to implement something like "floating view". To do this you should make sure
- The floating view is always the top most view in the hierarchy
- The position of the floating view should be updated when the
contentOffset
changes (so that visually it would float on top of other content when the user is scrolling)
You can try something like this (assuming that floatingHeaderViewCenter
is the initial center of the floatingView
:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.floatingHeaderView.center = CGPointMake(floatingHeaderViewCenter.x + scrollView.contentOffset.x, floatingHeaderViewCenter.y + scrollView.contentOffset.y);
[scrollView bringSubviewToFront:floatingHeaderView];
}
Although you'd better have a view container with two subviews: your tableview and the floating view.
UIView not appearing on top of UITableView
If you are intending for your view to scroll with the table content, I would make it the tableHeaderView
of the table. If you want the view to be static at the top, I would add it as a subview of the controller and then resize the table view frame to make room for the static view.
Note that if the controller is a UITableViewController
, self.view
is a UITableView
, so if you do something like [self.view addSubview:staticView]
this won't have the effect you expect.
Add top/bottom views to UITableViewController?
If you're wanting to use UITableViewController for it's abilities to manage cells and static layouts, the best possibility would be to use a container view in your storyboard. This would allow you to layout the main view contents around the table and still keep a separate UITableViewController maintaining the table itself. Set up the right auto layout constraints (really simple, just anchor the container view to the header and footer views)
Related Topics
Left-Align Image and Center Text on Uibutton
How to Detect Live Changes on Textfield in Swiftui
How to Populate Uitableview from the Bottom Upwards
Pod Error in Xcode "Id: Framework Not Found Pods"
Drop Cap with Nsattributedstring
Programmatically Get Path to Application Support Folder
What Do JSONserialization Options Do and How Do They Change JSONresult
Ios: Differencebetween -Init and -Viewload of a Viewcontroller
How to List All Classes Conforming to Protocol in Swift
Separation Between Header and First Cell -- in Plain Uitableview
Uicollectionview Horizontal Paging Not Centered
Swift:Missing Argument Label 'Xxx' in Call
How to Filter Nsarray Using Predicate on an Object Property
Making Video from Uiimage Array with Different Transition Animations
Error While Build Project Xcode Says:"You Don't Have Permission"