根据 距离 屏幕底边的距离 设置 控件的 y 坐标:
eg.
- Bug 内容:
在这个 viewController 上创建 tableView 时,不应该设置为分组样式:
UITableView
* tableView = [[
UITableView
alloc
]
initWithFrame
:[
UIScreen
mainScreen
].
bounds
style
:
UITableViewStyleGrouped
];
应该设置为:
UITableView
* tableView = [[
UITableView
alloc
]
initWithFrame
:[
UIScreen
mainScreen
].
bounds
];
然后再设置 tableView 的背景颜色;
Bug 原因:
设置为分组样式会导致出现组与组之间的默认距离,并且无法修改.
- 设置组的 header/footer 高度 :
#pragma mark -
组的
Header
高度
- (
CGFloat
)tableView:(
UITableView
*)tableView heightForHeaderInSection:(
NSInteger
)section{
return
0
;
}
#pragma mark -
组的
footer
的高度
-(
CGFloat
)tableView:(
UITableView
*)tableView heightForFooterInSection:(
NSInteger
)section{
return
0
;
}
- 设置 footerView 的 frame:
_footerView
.
frame
=
CGRectMake
(
0
,
0
,
KWIDTH
,
KHEIGHT
-
_headerView
.
height
-
44
-
64
);
注意:footerView/headerView 都是紧贴cell的,所以y值无作用;
- 添加控件:
添加时,根据高度计算出 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 = [[ UIBarButtonItem alloc ] initWithImage :[ UIImage imageNamed : @"product_button_back_nor.png" ] style : UIBarButtonItemStylePlain target : nil action : nil ];
self . navigationItem . leftBarButtonItem = leftItem;
self . navigationItem . title = @"NavigationController" ;
// 创建 tableView
UITableView * tableView = [[ UITableView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ]; // style:UITableViewStyleGrouped
tableView. backgroundColor = [ UIColor colorWithRed :( 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 . view addSubview :tableView];
}
#pragma mark - 懒加载 headerView
-( UIView *)headerView{
if (! _headerView ) {
_headerView = [[ UIView alloc ] init ];
// 创建 logo 图片
UIImageView * logoImageView = [[ UIImageView alloc ] init ];
logoImageView. backgroundColor = [ UIColor colorWithRed :(( 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
[ _headerView addSubview :logoImageView];
}
return _headerView ;
}
#pragma mark - 懒加载 footerView
- ( UIView *)footerView{
if (! _footerView ) {
_footerView =[[ UIView alloc ] init ];
_footerView . frame = CGRectMake ( 0 , 0 , KWIDTH , KHEIGHT - _headerView . height - 44 - 64 );
// _footerView.backgroundColor = [UIColor redColor];
// 用户交流 QQ 群
UILabel * userLabel = [[ UILabel alloc ] init ];
userLabel. text = @"DDDDDD:" ;
userLabel. font = [ UIFont systemFontOfSize : 18 ];
userLabel. textColor = [ UIColor colorWithHexString : @"#c7c7c7" ];
[userLabel sizeToFit ];
UILabel * QQLabel = [[ UILabel alloc ] init ];
QQLabel. text = @"QQXXXXXXXXXX" ;
QQLabel. font = [ UIFont systemFontOfSize : 18 ];
QQLabel. textColor = [ UIColor colorWithHexString : @"#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 = [[ UILabel alloc ] init ];
versionLabel. text = @"Xxxxxxx 222222" ;
versionLabel. textColor = [ UIColor colorWithHexString : @"#c7c7c7" ];
versionLabel. textAlignment = NSTextAlignmentCenter ;
versionLabel. font = [ UIFont systemFontOfSize : 18 ];
[versionLabel sizeToFit ];
versionLabel. centerX = KWIDTH * 0.5 ;
versionLabel. y = _footerView . height - 56 ;
[ _footerView addSubview :userLabel];
[ _footerView addSubview :versionLabel];
[ _footerView addSubview :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 = [tableView dequeueReusableCellWithIdentifier :aboutKey];
if (!cell) {
cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier :aboutKey];
}
cell. textLabel . text = @"XXXXXX" ;
cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator ;
cell. backgroundColor = [ UIColor whiteColor ];
return cell;
}
#pragma mark - 点击 cell 跳转至下一个控制器
- ( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath{
ZJHUserProtocolViewController * ptl = [[ ZJHUserProtocolViewController alloc ] init ];
[ self . navigationController pushViewController :ptl animated : 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 ;
}
#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 = [[ UIBarButtonItem alloc ] initWithImage :[ UIImage imageNamed : @"product_button_back_nor.png" ] style : UIBarButtonItemStylePlain target : nil action : nil ];
self . navigationItem . leftBarButtonItem = leftItem;
self . navigationItem . title = @"NavigationController" ;
// 创建 tableView
UITableView * tableView = [[ UITableView alloc ] initWithFrame :[ UIScreen mainScreen ]. bounds ]; // style:UITableViewStyleGrouped
tableView. backgroundColor = [ UIColor colorWithRed :( 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 . view addSubview :tableView];
}
#pragma mark - 懒加载 headerView
-( UIView *)headerView{
if (! _headerView ) {
_headerView = [[ UIView alloc ] init ];
// 创建 logo 图片
UIImageView * logoImageView = [[ UIImageView alloc ] init ];
logoImageView. backgroundColor = [ UIColor colorWithRed :(( 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
[ _headerView addSubview :logoImageView];
}
return _headerView ;
}
#pragma mark - 懒加载 footerView
- ( UIView *)footerView{
if (! _footerView ) {
_footerView =[[ UIView alloc ] init ];
_footerView . frame = CGRectMake ( 0 , 0 , KWIDTH , KHEIGHT - _headerView . height - 44 - 64 );
// _footerView.backgroundColor = [UIColor redColor];
// 用户交流 QQ 群
UILabel * userLabel = [[ UILabel alloc ] init ];
userLabel. text = @"DDDDDD:" ;
userLabel. font = [ UIFont systemFontOfSize : 18 ];
userLabel. textColor = [ UIColor colorWithHexString : @"#c7c7c7" ];
[userLabel sizeToFit ];
UILabel * QQLabel = [[ UILabel alloc ] init ];
QQLabel. text = @"QQXXXXXXXXXX" ;
QQLabel. font = [ UIFont systemFontOfSize : 18 ];
QQLabel. textColor = [ UIColor colorWithHexString : @"#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 = [[ UILabel alloc ] init ];
versionLabel. text = @"Xxxxxxx 222222" ;
versionLabel. textColor = [ UIColor colorWithHexString : @"#c7c7c7" ];
versionLabel. textAlignment = NSTextAlignmentCenter ;
versionLabel. font = [ UIFont systemFontOfSize : 18 ];
[versionLabel sizeToFit ];
versionLabel. centerX = KWIDTH * 0.5 ;
versionLabel. y = _footerView . height - 56 ;
[ _footerView addSubview :userLabel];
[ _footerView addSubview :versionLabel];
[ _footerView addSubview :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 = [tableView dequeueReusableCellWithIdentifier :aboutKey];
if (!cell) {
cell = [[ UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier :aboutKey];
}
cell. textLabel . text = @"XXXXXX" ;
cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator ;
cell. backgroundColor = [ UIColor whiteColor ];
return cell;
}
#pragma mark - 点击 cell 跳转至下一个控制器
- ( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath{
ZJHUserProtocolViewController * ptl = [[ ZJHUserProtocolViewController alloc ] init ];
[ self . navigationController pushViewController :ptl animated : 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"