iOS中的静态页脚

前端之家收集整理的这篇文章主要介绍了iOS中的静态页脚前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望让我的页脚浮动到表格视图以便它始终可见,而不仅仅是当我滚动到底部.

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

  1. - (id)initWithStyle:(UITableViewStyle)style
  2. {
  3. if ((self = [super initWithStyle: style]))
  4. {
  5. UIView *footer = [[UIView alloc] initWithFrame: CGRectMake(0,1,45)];
  6. footer.backgroundColor = [UIColor clearColor];
  7. self.tableView.tableFooterView = footer;
  8. self.tableView.frame = CGRectMake(0,320,100);
  9. footer.hidden = NO;
  10.  
  11. }

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

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

解决方法

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

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

>不理想但很简单:使用最后一部分的页脚视图作为静态页脚.这将在某些条件下工作:

>您的UITableView样式必须是UITableViewStylePlain(因为UITableViewStyleGrouped对UITableView tableHeaderView和tableFooterView的节头/页脚的行为相同
>您将无法使用最后一个部分页脚来实现其目的:为特定部分提供页脚信息

这是一个简单的例子:

  1. - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
  2. UIView *view = nil;
  3. if (section == [tableView numberOfSections] - 1) {
  4. // This UIView will only be created for the last section of your UITableView
  5. view = [[UIView alloc] initWithFrame:CGRectZero];
  6. [view setBackgroundColor:[UIColor redColor]];
  7. }
  8. return view;
  9. }

>现在最佳解决方案:在与UITableView相同的级别添加UIView(在代码中或在XIB中).一个小条件:

> UIViewController的self.view属性不能是您的UITableView对象.这意味着您不能将UITableViewController子类化为UIViewController,并使您的控制器符合UITableViewDataSource和UITableViewDelegate协议.它实际上比它看起来更简单,并且比直接使用UITableViewController更好的实现(就我而言).

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

ViewController.h:

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
  4.  
  5. @end

ViewController.m:

  1. #import "ViewController.h"
  2.  
  3. @interface ViewController ()
  4.  
  5. @property (strong,nonatomic) UITableView *tableView;
  6. @property (strong,nonatomic) UIView *fixedTableFooterView;
  7.  
  8. @end
  9.  
  10. @implementation ViewController
  11.  
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14.  
  15. CGFloat fixedFooterHeight = 200.0;
  16.  
  17. // Initialize the UITableView
  18. CGRect tableViewFrame = CGRectMake(CGRectGetMinX(self.view.bounds),CGRectGetMinY(self.view.bounds),CGRectGetWidth(self.view.bounds),CGRectGetHeight(self.view.bounds) - fixedFooterHeight);
  19. self.tableView = [[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain]; // or Grouped if you want...
  20. [self.tableView setDataSource:self];
  21. [self.tableView setDelegate:self];
  22. [self.view addSubview:self.tableView];
  23.  
  24. // Initialize your Footer
  25. CGRect footerFrame = CGRectMake(CGRectGetMinX(self.view.bounds),CGRectGetMaxY(self.view.bounds) - fixedFooterHeight,fixedFooterHeight); // What ever frame you want
  26. self.fixedTableFooterView = [[UIView alloc] initWithFrame:footerFrame];
  27. [self.fixedTableFooterView setBackgroundColor:[UIColor redColor]];
  28. [self.view addSubview:self.fixedTableFooterView];
  29. }
  30.  
  31. - (void)viewDidUnload {
  32. [super viewDidUnload];
  33. [self setTableView:nil];
  34. [self setFixedTableFooterView:nil];
  35. }
  36.  
  37. @end

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

警告:这个.h和.m文件将无法编译,因为我没有放入UITableViewDataSource所需的方法.如果要查看它的实际效果,请注释setDataSource:行.

希望这会有所帮助,

随意问我其他细节,

猜你在找的iOS相关文章