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 ()

@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

你可能感兴趣的:(iOS 头部视图下拉变大)