1. UIScrollView 实现 ViewPager 的效果(横向滑动切换页面)
新建RecommondViewController.m
#import "RecommendViewController.h"
@interface RecommendViewController ()
@end
@implementation RecommendViewController
- (instancetype)init{
if(self = [super init]){
self.view.backgroundColor = [UIColor blueColor];
self.tabBarItem.title = @"推荐";
self.tabBarItem.image = [UIImage imageNamed: @"icon.bundle/like@2x.png"];
self.tabBarItem.selectedImage = [UIImage imageNamed: @"icon.bundle/like_selected@2x.png"];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:self.view.bounds];
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width*5, self.view.bounds.size.height);
NSArray *colors = @[[UIColor redColor],[UIColor yellowColor],[UIColor blueColor],[UIColor blackColor],[UIColor greenColor]];
for(int i=0;i<5;i++){
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width*i, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
view.backgroundColor = colors[i];
[scrollView addSubview:view];
}
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.bounces = NO;
[self.view addSubview:scrollView];
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rQYGzkOn-1619711963604)(https://tva1.sinaimg.cn/large/008i3skNgy1gq0ligs4xng30k016gnq5.gif)]
2.自定义UITabViewCell
- 1.新建一个TestTableViewCell,重写它的构造方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier {
if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
[self.contentView addSubview:({
self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 15, 300, 50)];
self.titleLabel.font =[UIFont systemFontOfSize:16];
self.titleLabel.textColor =[UIColor blackColor];
self.titleLabel.backgroundColor =[UIColor redColor];
self.titleLabel;
})];
[self.contentView addSubview:({
self.sourceLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 80, 50, 20)];
self.sourceLabel.backgroundColor =[UIColor redColor];
self.sourceLabel.font =[UIFont systemFontOfSize:12];
self.sourceLabel.textColor =[UIColor blackColor];
self.sourceLabel;
})];
[self.contentView addSubview:({
self.commentLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 80, 50, 20)];
self.commentLabel.backgroundColor =[UIColor redColor];
self.commentLabel.font =[UIFont systemFontOfSize:12];
self.commentLabel.textColor =[UIColor blackColor];
self.commentLabel;
})];
[self.contentView addSubview:({
self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 80, 50, 20)];
self.timeLabel.backgroundColor =[UIColor redColor];
self.timeLabel.font =[UIFont systemFontOfSize:12];
self.timeLabel.textColor =[UIColor blackColor];
self.timeLabel;
})];
}
return self;
}
- UILabel 的自适应
-(void)layoutTableViewCell{
self.titleLabel.text = @"我是海贼王";
self.sourceLabel.text = @"路飞";
[self.sourceLabel sizeToFit];
self.commentLabel.text = @"娜美";
self.commentLabel.frame = CGRectMake(self.sourceLabel.frame.origin.x+self.sourceLabel.frame.size.width+15, self.sourceLabel.frame.origin.y, self.commentLabel.frame.size.width, self.commentLabel.frame.size.height);
[self.commentLabel sizeToFit];
self.timeLabel.text = @"山治";
self.timeLabel.frame = CGRectMake(self.commentLabel.frame.origin.x+self.commentLabel.frame.size.width+15, self.commentLabel.frame.origin.y, self.timeLabel.frame.size.width, self.timeLabel.frame.size.height);
[self.timeLabel sizeToFit];
}