这种布局是怎么元素布局的? UICollectionView 应该可以实现当然自己计算位置来实现一样可以来做。最底层的那个框直接切成一个图片,只需要关心上层icon及title的位置。可以定义一个model类来封装一下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我基本可以实现了,但是怎样让里面的格子完美居中?就像一个文本框可以设置center一样。http://www.cocoachina.com/bbs/read.php?tid=168709 我下面说的方案不是基于UICollectionView来实现的。每一个方格,你可以自定义一个控件,这个控件名定义为CustomCell ,这个cell接收一个实体model,包含icon及title的实体@interface CellModel : NSObject@property (nonatomic,strong) NSString *iconName;@property (nonatomic,strong) NSString *title;@end@class CellModel;@interface CustomCell: UIView@property (nonatomic,strong) CellModel *model;-(id)initWithModel:(CellModel *)cm;@end#import "CellModel.h"@implement CustomCell-(id)initWithModel:(CellModel *)cm { if (self=[super init]) { self.model=cm; } return self;}-(void)layoutSubview { if (nil!=self.model) { /////draw icon CGRect bounds=self.bounds; UIImage *iconImg=[UIImage imageNamed:self.model.iconName]; UIImageView *iconView=[[UIImageView alloc] initWithImage:iconImg]; /////compute position. set iconview's frame iconView.frame=CGRectMake((bounds.width-iconImg.size.width)/2, 5, iconImg.size.width,iconImage.size.height); [self addSubview:iconView]; //////draw title UILabel *lblTitle=[[UILabel alloc] init]; [lblTitle setTitle:self.model.title]; [lblTitle setColor;[UIColor black]]; [lblTitle setFont: [UIFont fontWithsize:12]]; [lblTitle setNumberOfLines:0]; [lblTitle setLineBreakModel:NSLineBreakWordWarpping]; CGSize titleSize=[lblTitle.text sizeWithFont:lblTitle.font]; lblTitle.frame=CGRectMake((bounds.width-titleSize.width)/2, CGRectGetMaxY(iconView.frame)+5, titleSize.width,titleSize.height); [self addSubview:lblTitle]; }}@end上面只是部分伪代码,纯手写,不保证完全正确。大体的思路应该是这样。 手写这么多,辛苦了。今天起床突然一开窍,发觉这个问题其实挺容易的。首先先把cell宽度定好了。cell直接没有空隙,这样所有的cell都平均的横向分布。然后再把里面的图片居中就好了。如下图所示:哥那么厉害有空再回答我个问题吧。请教类似日历类似效果的实现 http://www.cocoachina.com/bbs/read.php?tid=168789 iphone中模糊图片的指定区域的实现 iphone 文件操作的问题 【ios】十六进制 转换为 ASC 算法 用 PHP 和 XML 进行 iPhone 开发 豆瓣sdk 请教各位一个问题:facetime或者 企鹅 视频 中 远端的视频是如何展现的? 送福利! Swift语言学习资料汇总 iphone播放嵌入式网页视频 关于M7之前的计步器问题 IOS 推送DeviceToken 请教大家,回到主屏幕后,重新打开app,视图响应的是什么事件? 请问一下iphone手机能否通过数据线输出音频数据到功放或音响
我基本可以实现了,但是怎样让里面的格子完美居中?就像一个文本框可以设置center一样。
http://www.cocoachina.com/bbs/read.php?tid=168709
每一个方格,你可以自定义一个控件,这个控件名定义为CustomCell ,这个cell接收一个实体model,包含icon及title的实体
@interface CellModel : NSObject
@property (nonatomic,strong) NSString *iconName;
@property (nonatomic,strong) NSString *title;
@end@class CellModel;
@interface CustomCell: UIView
@property (nonatomic,strong) CellModel *model;-(id)initWithModel:(CellModel *)cm;@end
#import "CellModel.h"
@implement CustomCell-(id)initWithModel:(CellModel *)cm {
if (self=[super init]) {
self.model=cm;
} return self;
}-(void)layoutSubview {
if (nil!=self.model) {
/////draw icon CGRect bounds=self.bounds;
UIImage *iconImg=[UIImage imageNamed:self.model.iconName]; UIImageView *iconView=[[UIImageView alloc] initWithImage:iconImg];
/////compute position. set iconview's frame
iconView.frame=CGRectMake((bounds.width-iconImg.size.width)/2, 5, iconImg.size.width,iconImage.size.height);
[self addSubview:iconView]; //////draw title
UILabel *lblTitle=[[UILabel alloc] init];
[lblTitle setTitle:self.model.title];
[lblTitle setColor;[UIColor black]];
[lblTitle setFont: [UIFont fontWithsize:12]];
[lblTitle setNumberOfLines:0];
[lblTitle setLineBreakModel:NSLineBreakWordWarpping]; CGSize titleSize=[lblTitle.text sizeWithFont:lblTitle.font]; lblTitle.frame=CGRectMake((bounds.width-titleSize.width)/2, CGRectGetMaxY(iconView.frame)+5, titleSize.width,titleSize.height);
[self addSubview:lblTitle]; }
}@end上面只是部分伪代码,纯手写,不保证完全正确。大体的思路应该是这样。