iOS中的静态页脚

use*_*929 7 footer uitableview ios

我想让我的页脚浮动在桌面视图上这样它总是可见的,不仅当我滚动到底部.

我已尝试使用此代码调整表格视图的大小:

- (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;

    }
Run Code Online (Sandbox Code Playgroud)

到目前为止,我没有运气.

我也尝试过使用其他窗口并调整所有.XIB文件的大小.

Zed*_*nem 15

A UITableViewtableFooterView属性是一个UIView始终显示在内容底部,最后一部分下方的对象.即使在Apple的文档中并不是很清楚(摘录:"返回显示在表格下方的附件视图.").

如果您希望页脚是静态和非浮动的,您有两个简单的选择:

  • 不理想但很简单:使用最后一部分的页脚视图作为静态页脚.这将在某些条件下工作:
    • 你的UITableView风格必须是UITableViewStylePlain(因为UITableViewStyleGrouped给和/ UITableView tableHeaderView和节的页眉/页脚的行为相同)tableFooterView
    • 您将无法使用最后一个部分页脚来实现其目的:为特定部分提供页脚信息

这是一个简单的例子:

- (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;
}
Run Code Online (Sandbox Code Playgroud)
  • 现在最好的解决方案:添加一个UIView(在代码中或在您的XIB中)与您的相同级别UITableView.一个小条件:
    • self.view您的财产UIViewController不得成为您的UITableView对象.这意味着你不能继承UITableViewController,但是UIViewController,让你的控制器符合UITableViewDataSourceUITableViewDelegate协议.它实际上比它看起来更简单,更好的实现(就我而言)比直接使用UITableViewController.

这是代码中的一个简单示例(但您可以使用Interface Builder完全相同):

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

您还可以指定UIViewAutoresizing蒙版以使其在纵向和横向上无缝工作,但我不会使这个相当简单的代码复杂化.

警告:此.h和.m文件将无法编译,因为我没有放入UITableViewDataSource所需的方法.setDataSource:如果您想要查看该行,请对该行进行注释.

希望这会有所帮助,

随意问我其他细节,


Joh*_*ijk -3

一个非常hacky的解决方案,但这会起作用:

UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
[keyWindow.viewForBaselineLayout addSubview:footer];
Run Code Online (Sandbox Code Playgroud)

请注意,这将隐藏主窗口中将重叠的所有内容。