标签:去掉tableview空白 tableview顶部空白
经常遇到tableView上面会有空白的问题:
见下图:
可以看到上图的tableView顶部有一个空白的区域:
为什么?(如果直接用的是tableView不会出现此问题!)
这可能是由你 在 ViewController添加 tableView和Navigation的顺序的 有问题:
正常的顺序应该是 添加好view 添加 tableView之后 ,添加约束,然后再添加Navigation
但是万一你的tableView出现了 上图那样的空白如何解决呢?
方法1.让tableVIew上移64,为什么是 64呢?因为 那个宽度就是一个 Navagation的高度 64(竖屏时)
加上如下代码:
self.tableView.contentInset=UIEdgeInsetsMake(-64, 0, 0, 0);
让你的tableView按照 上 ,左 ,下 ,右的方向 偏移
实际上这个 方法 在collectionView中也经常使用!完整代码:
//添加悬浮按钮的代码,外加调整tableView的 偏移量
//
// ViewController.m
// StoneButton
//
// Created by http://blog.csdn.net/yangbingbinga on 14/12/26.
// Copyright (c) 2014年 http://blog.csdn.net/yangbingbinga. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
<strong><span style="font-size:24px;">self.tableView.contentInset=UIEdgeInsetsMake(-64, 0, 0, 0);//上移64</span></strong>
UIScreen *s=[UIScreen mainScreen];
CGFloat width=s.bounds.size.width;
CGFloat height=s.bounds.size.height;
CGRect frame=CGRectMake(15, height-62, width-30, 50);
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"悬浮按钮" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor redColor]];
[button setFrame:frame];
button.layer.cornerRadius=5;
button.clipsToBounds=YES;
[self.view addSubview:button];
[self.view bringSubviewToFront:button];
CGRect frame2=CGRectMake(15, 400, 50, 150);
UIButton *button2=[UIButton buttonWithType:UIButtonTypeCustom];
[button2 setTitle:@"悬浮按钮2" forState:UIControlStateNormal];
[button2 setBackgroundColor:[UIColor grayColor]];
[button2 setFrame:frame2];
button2.layer.cornerRadius=5;
button2.clipsToBounds=YES;
[self.view addSubview:button2];
[self.view bringSubviewToFront:button2];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
long row=indexPath.row%3;
UITableViewCell *cell;
NSString *idf;
if (row==0) {
idf=@"a";
}
else if(row==1)
{
idf=@"b";
}
else
{
idf=@"c";
}
cell = [tableView dequeueReusableCellWithIdentifier:idf forIndexPath:indexPath];
return cell;
}
@end
可以看到刚才的空白 顺利解决!Ok,需要注意的是:
<strong><span style="font-size:24px;">UIEdgeInsetsMake(-64, 0, 0, 0);</span></strong>
根据实际需求调整这4个值!
2.修改约束值:
将tableView的 top 边界约束上移64:
self.tableViewTopCons.constant-=64;
完整代码:
//
// ViewController.m
// StoneButton
//
// Created by http://blog.csdn.net/yangbingbinga on 14/12/26.
// Copyright (c) 2014年 http://blog.csdn.net/yangbingbinga. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *tableViewTopCons;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
<span style="font-size:24px;"> self.tableViewTopCons.constant-=64;//修改 tableView的top约束,上移64!</span>
UIScreen *s=[UIScreen mainScreen];
CGFloat width=s.bounds.size.width;
CGFloat height=s.bounds.size.height;
CGRect frame=CGRectMake(15, height-62, width-30, 50);
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"悬浮按钮" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor redColor]];
[button setFrame:frame];
button.layer.cornerRadius=5;
button.clipsToBounds=YES;
[self.view addSubview:button];
[self.view bringSubviewToFront:button];
CGRect frame2=CGRectMake(15, 400, 50, 150);
UIButton *button2=[UIButton buttonWithType:UIButtonTypeCustom];
[button2 setTitle:@"悬浮按钮2" forState:UIControlStateNormal];
[button2 setBackgroundColor:[UIColor grayColor]];
[button2 setFrame:frame2];
button2.layer.cornerRadius=5;
button2.clipsToBounds=YES;
[self.view addSubview:button2];
[self.view bringSubviewToFront:button2];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
long row=indexPath.row%3;
UITableViewCell *cell;
NSString *idf;
if (row==0) {
idf=@"a";
}
else if(row==1)
{
idf=@"b";
}
else
{
idf=@"c";
}
cell = [tableView dequeueReusableCellWithIdentifier:idf forIndexPath:indexPath];
return cell;
}
@end
更多原文参见:http://blog.csdn.net/yangbingbinga/article/details/43075095标签:去掉tableview空白 tableview顶部空白
原文地址:http://blog.csdn.net/yangbingbinga/article/details/43322417