Custom header view overlapped by UITableView - ios

In interface builder I have a header view above my UITableView, however in the simulator it is missing, and the Table View seems to be over it since it takes up most of the screen. Any reason for this? Work around?
Interface Builder
Simulator

In your case:
From your interface builder image I can see that table view is subview of view and you header view is also subview of tableview. It happening because tableview is hiding the header view. if you add the header view as subview of tableview then it would appear. But adding subview will not solve your problem because when you will scroll the tableview the subview will be gone.
For solving your problem as I can see that you have a navigation bar in your table view you can add the header view as subview of UINavigationBar.
[self.navigationController.view addSubview:headerView];
Good Practise:
Set the header view for section. Then you don't need to set any custom view at the top of your tableview. Try this:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *aView = [[UIView alloc] init];
//Customize the view according to our requirment
return aView;
}
Also implement this:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44.f; // Make this height as you need. Or else you may not see the full view.
}
Btw you can create a subclass of UITableViewController instead of UIViewController.
Hope this helps.. :)

Deselect Extend Edges / Under Top Bars

Related

Where should I add my subview to have it UNDER my table view in a UITableViewController?

I'm using parse's PFQueryTableViewController, which is almost exactly the same as a UITableViewController except it handles some querying in the background.
My goal is to place a static toolbar at the bottom of the screen. I decided to do this by giving tableView a smaller frame, and creating and adding my toolbar subview below the tableView, as a subview to the same view that the tableView is a subview of.
Here's a photo. I am trying to add my toolbar to the little white space at the bottom of the screen. That whitespace does NOT scroll with the table view:
I have tried [self.view addSubview: toolbar], however, this just adds it to the tableView, and as a result, my toolbar scrolls with it. I can't seem to find any documentation on the view hierarchy for a UITableViewController. Anyone know where I should add my toolbar subview? I should also mention that all of this is sitting inside of a UINavigationController.
A PFQueryTableViewController (like all UITableViewControllers) has a table view as its view.
If you want to have other views within the view controller, either don't use a table view controller, or subclass UINavigationController.
Alternatively, you can set the view controller's toolbarItems property, and the navigation controller will take care of making and configuring a standard toolbar.
implement viewFAorHeaderInSection delegate method of UITableView:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 90)];
customView.backgroundColor = [UIColor blackColor];
return customView;
}
and heightForFooterInSection
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 80;
}
it should solove your problem.

Is it possible to have a fixed uitableview Header while using sections?

This question should not be mixed up with this here.. These are two different things.
There is a good example how to use a UITableView Header on SO.
This all works fine and the main header is fixed on top as long as the style is set to plain.
But if I use sections, the main header no longer sticks to top and moves away while scrolling to the bottom.
In this method, I am returning the header for each section.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
In this method I am setting the height for the header section above:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
In this method, I am setting the real table header.
- (void)viewWillAppear:(BOOL)animated {
...
self.recordTableView.tableHeaderView = headerView;
}
Is it even possible having a fixed table header, while using sections?
What is an alternative solution to this please?
If you want a UITableViewController (static cells/keyboard handling) and have a fixed header then you should use Containment. You can do this from a Storyboard by setting up a UIViewController with your fixed header and then using a Container View to embed the UITableViewController.
Once you have your containing view setup, you right-click drag from the Container View to the View Controller you want to embed - the UITableViewController in this case.
You can access and get a reference to the contained View Controller (the UITableViewController) from the Container View Controller by implementing the prepareForSegue:sender: method.
There’s no way to maintain the header of a tableView fixed, but
an useful approach when you need a unique header, is to use a UIViewController rather than a UITableViewController, and set the header (UIView) out from the tableView.
Something like this:
If you want to keep the class as a UITableViewController you can add your header as a subview to the tableview's superview. You will have to also push the tableview top inset down so your headerview doesnt hide the table.
Here is a sample code to put inside your tableViewController subclass (This example assumes your tableview controller is inside a navigation controller, so it pushes the view to below the navigation bar):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.automaticallyAdjustsScrollViewInsets = NO;
}
-(void)addHeaderView{
CGFloat yPosition = self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height;
mainHeaderView = [[UIView alloc] init];
const CGFloat mainHeaderHeight = 44;
[mainHeaderView setFrame:CGRectMake(0, yPosition, self.view.frame.size.width, mainHeaderHeight)];
mainHeaderView.backgroundColor = [UIColor redColor];
[self.tableView.superview addSubview:mainHeaderView];
[self.tableView setContentInset:UIEdgeInsetsMake(yPosition + mainHeaderHeight, self.tableView.contentInset.left, self.tableView.contentInset.bottom, self.tableView.contentInset.right)];
}
I haven't done this, but the first thing I would think to try is to place my tableview in a UIView and make my own header there in that UIView. Seems a trivial matter to make that view appear to be the header of the table and it would certainly stay put.

Custom UITableView Footer doesn't lock in the bottom as the table view is scrolled

I have checked all these
UITableView, make footer stay at bottom of screen?
tableFooterView property doesn't fix the footer at the bottom of the table view
iOS - viewForFooterInSection sticking to bottom of UITableView
Proper way to implement a footer in UITableView
similar questions but unfortunately my problem hasn't resolved.
I have to implement a custom header and footer views with buttons inside. I have created separate UIView's subclasses with .nib files. In my view controller, I'm calling these methods to register nibs for header and footer view.
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CustomTableHeaderView *view = [CustomTableHeaderView header];
view.delegate = self; //setting delegate to receive callbacks as the buttons inside the view are pressed
return view;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
CustomTableFooterView *view = [CustomTableFooterView footer];
view.delegate = self;
return view;
}
Where as the class method in the custom views registers a .nib file and returns the view. However the implementation is;
+ (CustomTableHeaderView*)header
{
return [[[NSBundle mainBundle]loadNibNamed:#"CustomTableHeaderView" owner:nil options:nil]objectAtIndex:0];
}
Similar implementation for footer.
The problem is that the footer view doesn't lock at the bottom when the table view scrolls. i-e, when there are more rows to fit inside the view, the footer view hides and is revealed when all the rows are scrolled down till the end. I want to lock the footer view at the bottom of the view no matter how much rows are there to scroll.
The header view has been implemented perfectly by this implementation as it is locked at the top while the rows are being scrolled, however the footer view is scrolled with the rows.
I have also tried self.tableview.tablefooterview property but it didn't help either.
Unfortunately thats not how table section footers work. In order to accomplish an anchored view at the bottom you will need to add it as a subview to your UIView manually.
If you add it as a subview to your UITableView you will need to keep it anchored by changing its frame in scrollViewDidSroll:. If you add it as a subview to the UIView containing your UITableView you can just place it statically at the bottom. In either case you probably want to adjust the contentInset of the table view with an inset at the bottom so that you can scroll your content up above the anchored footer.

Header in storyboard for UITableViewController does not "stick"

I have a UITableViewController that is in a storyboard. I want to add a header to it so I followed the instructions in the following post. I dragged a UIView up on top, and on top of that i dragged a couple of images.
Table Header Views in StoryBoards
Now that seemed to work fine, but the header scrolls along with the entries in table cells when i scroll up. Also, they seem to scroll underneath the time and battery indicator...not sure why that is.
So in the comments of that post i saw that you need to implement the following function to return the UiView to get it to "stick". The only way I saw how to get the UiView from the storyboard was to set the tag and then look it up, also shown below.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section.
{
int tagNumber = 2;
UIView *headerView = (UIView *)[self.view viewWithTag:tagNumber];
return headerView;
}
The issue with all of this is the header still scrolls. How can i get the header view to just stay on top?
Thanks!

Show a message on top of a UITableView

I have a UITableView which is backed by a NSFetchedResultsController. I would like to display a message on top of the list, e.g. "Showing items filtered by XYZ". I tried inserting a UIView with a UILabel in it on top of the screen in the IB, but it doesn't seem to work. Is this even possible to do what I want?
If you don't use header on the table view, your can add a custom header on the top of your UITableView.
Implement this method :
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *myCustomView = [UIView alloc] init];
//Add your label on myCustomView
return myCustomView;
}
My guess is that your .xib's owner is subclassing from a UITableViewController, try changing it to a UIViewController that has a UITableView and your UILabel as subviews in IB.

Resources