7

Im Looking to get my footer to Float over a table view So that it is Always Visible and not only when i scroll to the bottom.

i have tried resizing the table view using this code :

- (id)initWithStyle:(UITableViewStyle)style
{
    if ((self = [super initWithStyle: style]))
    {
        UIView *footer = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 1, 45)];
        footer.backgroundColor = [UIColor clearColor];
        self.tableView.tableFooterView = footer;
        self.tableView.frame = CGRectMake(0, 0, 320, 100);
        footer.hidden = NO;

    }

I have had no luck thus far.

I have also tried Using other windows and resizing all the .XIB files.

user2575929
  • 71
  • 1
  • 1
  • 2
  • Using this code it will only display when you scroll to the bottom of tableview. if you want t make it visible all the time than create small view and place it below the tableview. that will do it. – Dilip Manek Jul 12 '13 at 10:05

4 Answers4

15

A UITableView's tableFooterViewproperty is a UIViewobject that is always displayed at the bottom of the content, below the last section. Even if that is not really clear in Apple's documentation (extract : "Returns an accessory view that is displayed below the table.").

If you want the footer to be static and non-floating, you have two easy choices :

  • Not ideal but simple : Use the last section's footer view as your static footer. This will work at some conditions :
    • your UITableView style must be UITableViewStylePlain (as UITableViewStyleGroupedgives the same behaviour to section headers/footers as to the UITableView tableHeaderView and tableFooterView
    • You won't be able to use the last section footer for its real purpose : giving footer informations to a specific section

Here is a simple example :

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = nil;
    if (section == [tableView numberOfSections] - 1) {
        // This UIView will only be created for the last section of your UITableView
        view = [[UIView alloc] initWithFrame:CGRectZero];
        [view setBackgroundColor:[UIColor redColor]];
    }
    return view;
}
  • Best solution right now : Add a UIView (either in code or in your XIB) at the same level as your UITableView. One little condition :
    • The self.view property of your UIViewController must not be your UITableView object. That means you can't subclass UITableViewController but UIViewController and make your controller conform to UITableViewDataSource and UITableViewDelegate protocols. It's actually simpler than it looks and a better implementation (as far as I'm concerned) than using directly UITableViewController.

Here is a simple example in code (but you can do exactly the same using Interface Builder) :

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@end

ViewController.m :

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIView *fixedTableFooterView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CGFloat fixedFooterHeight = 200.0;

    // Initialize the UITableView
    CGRect tableViewFrame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - fixedFooterHeight);
    self.tableView = [[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain]; // or Grouped if you want...
    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];
    [self.view addSubview:self.tableView];

    // Initialize your Footer
    CGRect footerFrame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMaxY(self.view.bounds) - fixedFooterHeight, CGRectGetWidth(self.view.bounds), fixedFooterHeight); // What ever frame you want
    self.fixedTableFooterView = [[UIView alloc] initWithFrame:footerFrame];
    [self.fixedTableFooterView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:self.fixedTableFooterView];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    [self setTableView:nil];
    [self setFixedTableFooterView:nil];
}

@end

You can also specify UIViewAutoresizing mask to make it work seamlessly in portrait and landscape but I didn't to complicate this rather simple code.

WARNING : This .h and .m files won't compile as I didn't put in the UITableViewDataSource required methods. Comment the setDataSource: line if you want to see it in action.

Hope this will help,

Feel free to ask me other details,

Zedenem
  • 2,479
  • 21
  • 23
  • Same as this question : http://stackoverflow.com/questions/15687370/tablefooterview-property-doesnt-fix-the-footer-at-the-bottom-of-the-table-view?rq=1 – Zedenem Jul 12 '13 at 10:41
0

You can add UIView under the UITableView and it will be always visible.

If you need lot of customisation, just add UITableView to UIViewController instead of using UITableViewController.

BSMP
  • 4,596
  • 8
  • 33
  • 44
Vojtech Vrbka
  • 5,342
  • 6
  • 44
  • 63
  • Yes but its possible in case of UIViewController only. If my view is UITableViewController then ? – Adeel Apr 25 '16 at 08:03
  • 1
    If you need lot of customisation, just add UITableView to UIViewController instead of using UITableViewController – Vojtech Vrbka Apr 25 '16 at 10:28
  • Yes This seems to be ultimate solution to support all the required features in a seamless manner. Any way thanks dear :) – Adeel Apr 26 '16 at 03:44
  • there still remains 1 issue in this solution, that I want to use static cells in the table view, which can be used in a UITableView, without using UITableViewController. – MhmdRizk Nov 14 '18 at 08:20
0

Try this, it will keep your view alway at the bottom of the view:

footerView.transform = CGAffineTransformMakeTranslation(0, scrollView.contentOffset.y)
Pablo A.
  • 2,042
  • 1
  • 17
  • 27
-3

A very hacky solution, but this will work :

UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
[keyWindow.viewForBaselineLayout addSubview:footer];

Beware, this will hide everything in your main window that it will overlap.

JohnVanDijk
  • 3,546
  • 1
  • 24
  • 27