Static footer in iOS

Im looking to have my footer float above the table view so that it is always visible, and not only when I scroll the bottom.

I 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 still have no luck.

I also tried using other windows and resized all .XIB files.

+7
ios uitableview footer
source share
4 answers

A UITableView tableFooterView property is a UIView object that always appears at the bottom of the content, below the last section. Even if this is not entirely clear in the Apple documentation (extract: "Returns the auxiliary view that appears below in the table.").

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

  • Not perfect, but simple: use the footer of the last section as your static footer. This will work under certain conditions:
    • your UITableView style should be UITableViewStylePlain (since UITableViewStyleGrouped gives the same behavior for section headers / footers as for UITableView tableHeaderView and tableFooterView
    • You won’t be able to use the footer of the last section for your real purpose: providing footer information in 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; } 
  • The best solution right now: add a UIView (either to the code or to your XIB) at the same level as your UITableView . One small condition:
    • The self.view property of your UIViewController should not be your UITableView object. This means that you cannot subclass UITableViewController , but UIViewController and combine your controller with the UITableViewDataSource and UITableViewDelegate protocols. This is actually simpler than it sounds and a more efficient implementation (as far as I know) than using the UITableViewController directly.

Here is a simple example in the code (but you can do the same with 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 a UIViewAutoresizing mask UIViewAutoresizing that it works smoothly in portrait and landscape orientation, but I did not complicate this rather simple code.

A WARNING. These .h and .m files will not compile since I did not apply the required UITableViewDataSource methods. Comment out the setDataSource: line if you want to see it in action.

Hope this helps,

Feel free to ask me about other details,

+15
source share

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

If you need a lot of customization, just add a UITableView to the UIViewController instead of using the UITableViewController.

0
source share

Try this, it will be constantly displayed at the bottom of the view:

 footerView.transform = CGAffineTransformMakeTranslation(0, scrollView.contentOffset.y) 
0
source share

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.

-3
source share

All Articles