Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static footer in iOS

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.

like image 509
user2575929 Avatar asked Jul 12 '13 09:07

user2575929


1 Answers

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,

like image 132
Zedenem Avatar answered Nov 06 '22 11:11

Zedenem