一个关于tableView的footerView的子控件y 坐标的 BUG

来源:互联网 发布:人体润滑液 知乎 编辑:程序博客网 时间:2024/06/04 01:37
根据 距离 屏幕底边的距离 设置 控件的 y 坐标:
eg.


  1. Bug 内容:
在这个 viewController 上创建 tableView 时,不应该设置为分组样式:
UITableView * tableView = [[UITableView alloc] initWithFrame:[UIScreenmainScreen].bounds style:UITableViewStyleGrouped];

应该设置为:
UITableView * tableView = [[UITableView alloc] initWithFrame:[UIScreenmainScreen].bounds ];

然后再设置 tableView 的背景颜色;
Bug 原因:
设置为分组样式会导致出现组与组之间的默认距离,并且无法修改.

  1. 设置组的 header/footer 高度 :
#pragma mark - 组的 Header 高度- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return0;
}


#pragma mark - 组的 footer 的高度-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return0;
}

  1. 设置  footerView 的 frame:
_footerView.frame = CGRectMake(0, 0, KWIDTH,KHEIGHT - _headerView.height - 44 - 64);
注意:footerView/headerView 都是紧贴cell的,所以y值无作用;

  1. 添加控件:
添加时,根据高度计算出 y 坐标(坐标是相对于 footerView 的 y 坐标,而不是整个屏幕的 y 坐标):
versionLabel.y = _footerView.height - 56;





附:
1.整个类文件代码:

#import"ZJHAboutViewController.h"
#import
"ZJHUserProtocolViewController.h"
#import
"UIView+Extension.h"
#import
"UIColor+Helpers.h"

#define kWIDTH [UIScreen mainScreen].bounds.size.width

@interface ZJHAboutViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong,nonatomic)UIView* headerView;

@property (strong,nonatomic)UIView* footerView;

@end

@implementation ZJHAboutViewController
#pragma mark - 关于产品类


- (
void)viewDidLoad {
    [
super viewDidLoad];
   
// Do any additional setup after loading the view.
   
   
// nav 的左侧按钮 标题
   
UIBarButtonItem*leftItem = [[UIBarButtonItemalloc]initWithImage:[UIImageimageNamed:@"product_button_back_nor.png"]style:UIBarButtonItemStylePlaintarget:nilaction:nil];
   
   
self.navigationItem.leftBarButtonItem= leftItem;
   
self.navigationItem.title=@"NavigationController";
   
   
// 创建 tableView
   
UITableView* tableView = [[UITableViewalloc]initWithFrame:[UIScreenmainScreen].bounds];// style:UITableViewStyleGrouped
    tableView.
backgroundColor= [UIColorcolorWithRed:(float)(242/250.0)green:(float)(242/250.0)blue:(float)(242/250.0)alpha:1];
   
    tableView.
delegate=self;
    tableView.
dataSource=self;
    tableView.
tableHeaderView=self.headerView;
    tableView.
tableFooterView=self.footerView;
   
//    tableView.scrollEnabled = NO;
   
    [
self.viewaddSubview:tableView];
}



#pragma mark - 懒加载 headerView
-(UIView*)headerView{
   
   
if (!_headerView) {
       
_headerView= [[UIViewalloc]init];
       
       
// 创建 logo 图片
       
UIImageView* logoImageView = [[UIImageViewalloc]init];
        logoImageView.
backgroundColor= [UIColorcolorWithRed:((float)arc4random_uniform(256)/255.0)green:((float)arc4random_uniform(256)/255.0)blue:((float)arc4random_uniform(256)/255.0)alpha:1];
        logoImageView.
centerX= (375-150) *0.5;
        logoImageView.
y=30;
        logoImageView.
width=150;
        logoImageView.
height=150;
       
       
       
// 设置 HeaderView 大小
       
_headerView.frame=CGRectMake(0,0,kWIDTH, logoImageView.y+ logoImageView.height+30);
       
       
NSLog(@"%f",_headerView.height);
       
       
// 添加 logo
        [
_headerViewaddSubview:logoImageView];
       
    }
   
return _headerView;
}


#pragma mark - 懒加载 footerView
- (UIView*)footerView{
   
   
if (!_footerView) {
       
_footerView=[[UIViewalloc]init];
       
_footerView.frame=CGRectMake(0,0,KWIDTH,KHEIGHT-_headerView.height-44- 64);
//        _footerView.backgroundColor = [UIColor redColor];

       
       
// 用户交流 QQ
       
UILabel* userLabel = [[UILabelalloc]init];
        userLabel.
text=@"DDDDDD:";
        userLabel.
font= [UIFontsystemFontOfSize:18];
        userLabel.
textColor= [UIColorcolorWithHexString:@"#c7c7c7"];
        [userLabel
sizeToFit];
       
       
UILabel* QQLabel = [[UILabelalloc]init];
        QQLabel.
text=@"QQXXXXXXXXXX";
        QQLabel.
font= [UIFontsystemFontOfSize:18];
        QQLabel.
textColor= [UIColorcolorWithHexString:@"#07c0c0"];
        [QQLabel
sizeToFit];
       
        userLabel.
x= (KWIDTH- userLabel.width- QQLabel.width) *0.5;
        QQLabel.
x= userLabel.x+ userLabel.width;
        userLabel.
y=_footerView.height-88;
        QQLabel.
y=_footerView.height-88;
       
       
       
// 版本号
       
// 创建爱心标语
       
UILabel* versionLabel = [[UILabelalloc]init];
        versionLabel.
text=@"Xxxxxxx  222222";
        versionLabel.
textColor= [UIColorcolorWithHexString:@"#c7c7c7"];
        versionLabel.
textAlignment=NSTextAlignmentCenter;
        versionLabel.
font= [UIFontsystemFontOfSize:18];
        [versionLabel
sizeToFit];
        versionLabel.
centerX=KWIDTH* 0.5;
        versionLabel.
y=_footerView.height-56;
       
       
        [
_footerViewaddSubview:userLabel];
        [
_footerViewaddSubview:versionLabel];
        [
_footerViewaddSubview:QQLabel];
       
    }
   
return _footerView;
}


#pragma mark - UItableView的数据源代理方法
// 一共多少行
- (
NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
   
return 1;
}
// 每行长啥样
- (
UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
   
   
static NSString * aboutKey = @"aboutKey";
   
   
UITableViewCell* cell = [tableViewdequeueReusableCellWithIdentifier:aboutKey];
   
   
if (!cell) {
        cell = [[
UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:aboutKey];
    }
   
    cell.
textLabel.text=@"XXXXXX";
   
    cell.
accessoryType=UITableViewCellAccessoryDisclosureIndicator;
   
    cell.
backgroundColor= [UIColorwhiteColor];
   
   
return cell;
}


#pragma mark - 点击 cell 跳转至下一个控制器
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
   
   
ZJHUserProtocolViewController* ptl = [[ZJHUserProtocolViewControlleralloc]init];
   
    [
self.navigationControllerpushViewController:ptlanimated:YES];
}


#pragma mark - 组的 Header 高度
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
   
return 0;
}


#pragma mark - 组的 footer 的高度
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{
   
return 0;
}

@end







2.分类:
设置控件xy长宽分类:#import "UIView+Extension.h"

根据十六进制设置颜色分类:#import "UIColor+Helpers.h"

0 0