iOS 头部视图下拉变大

都在代码里,直接上代码

//改变tableView的内边距
self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
//这里创建的视图Y和高没有卵用,希望知道的同志告知一下
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -200, self.view.frame.size.width, 0)];
    self.headerView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:.8];
//在这里创建的视图一定要添加到tableView上,而不是给头视图赋值
    [self.tableView addSubview:self.headerView];

*****
想要宽高等比例缩放加上这句代码
self.headerView.contentMode = UIViewContentModeScaleToFill;

在scrollView代理方法里实现视图大小的改变

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat yOffset  = scrollView.contentOffset.y;
    if (yOffset < -200) {
        CGRect f = self.headerView.frame;
        f.origin.y = yOffset;
        f.size.height =  -yOffset;
        self.headerView.frame = f;
    }
  
}

仿支付宝的刷新简陋版

#import "ViewController.h"
#import "MJRefresh.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic)UITableView *rootTableView;

@property(strong,nonatomic)UIView *topView;

@property(strong,nonnull)UIView *backView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    
    [self.view addSubview:self.rootTableView];
    
    [self.view addSubview:self.topView];
   
}

//懒加载tabbleView
-(UITableView *)rootTableView{

    if (!_rootTableView) {
        
        _rootTableView = [[UITableView alloc] initWithFrame:self.view.frame];
        
        //设置代理
        _rootTableView.delegate = self;
        _rootTableView.dataSource = self;
        
        _rootTableView.scrollIndicatorInsets = UIEdgeInsetsMake(200, 0, 0, 0);
        _rootTableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{           
                [_rootTableView.mj_header endRefreshing];      
            });          
        }];
        _rootTableView.mj_header = header;
    }
    
    return _rootTableView;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat offsetY = scrollView.contentOffset.y;
    NSLog(@"%f",offsetY);
    if (offsetY <= -263) {
//
        self.topView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 200);
    }
    if (offsetY > -263){

        self.topView.frame = CGRectMake(0, -offsetY - 200  , [UIScreen mainScreen].bounds.size.width, 200);
    }
    
}
-(UIView *)topView{

    if (!_topView) {
        
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
        
        _topView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:.3];
        
    }

    return _topView;

}
#pragma make  代理方法

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 20;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [UITableViewCell new];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    
    return cell;

}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,752评论 25 709
  • 翻译自“Collection View Programming Guide for iOS” 0 关于iOS集合视...
    lakerszhy阅读 4,038评论 1 22
  • 《跃迁》第三章联机学习中有一段让我印象深刻。一个想出国留学的学生希望得到国外大学的录取通知,通过来回搬运国外大学教...
    helenxxf阅读 174评论 0 2
  • 不知道该怎么了结,想想还是算了。 人生,都是从苦里熬出来的。熬过了必须的苦,或许才能过上喜欢的生活吧。 “等你们长...
    沐雨之鸢阅读 203评论 0 0
  • 上帝告诉我:“你们都是一棵开着花的树。 我说:“不,我们会成为一棵高耸入云的云树。 上帝笑着走开了。
    琥珀王奕阅读 321评论 0 1

友情链接更多精彩内容