/**
* 主要思路
1.用kvo监听expandview的拉伸
2.拉伸时候让expangviewframe变化的同时设置充满整个屏幕
*/
controller 中
- (void)viewDidLoad {
[superviewDidLoad];
UIImageView *imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(0,0, 375*kWidth,180*kHeight)];
[imageView setImage:[UIImageimageNamed:@"image"]];
/**
* 这里也可以自定义一个view不一定要一个imageview 自定义时候一定要设置imageview属性
*/
/*
//关键步骤设置可变化背景view属性
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight| UIViewAutoresizingFlexibleWidth;
imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFill;
[customView addSubview:imageView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
[label setText:@"这是一个自定义头部view"];
[label setTextColor:[UIColor redColor]];
[customView addSubview:label];
*/
_tableView = [[UITableViewalloc] initWithFrame:CGRectMake(0,0,375*kWidth, 667*kHeight) style:UITableViewStylePlain];
;
_tableView.delegate =self;
_tableView.dataSource =self;
[self.viewaddSubview:_tableView];
_header = [SunExpandHeadernew];
/**
* 这里_tableView只要是scrollview子类就可以
*/
[_headerexpandWithScrollView:_tableViewexpandView:imageView];
}
封装的SunExpandHeader
-(void)expandWithScrollView:(UIScrollView *)scrollView expandView:(UIView *)expandView
{
_expandHeight =CGRectGetHeight(expandView.frame);
_scrollView = scrollView;
_scrollView.contentInset =UIEdgeInsetsMake(_expandHeight,0, 0,0);
[_scrollViewinsertSubview:expandView atIndex:0];
[_scrollViewaddObserver:selfforKeyPath:SunExpandContentOffsetoptions:NSKeyValueObservingOptionNewcontext:nil];
_expandView = expandView;
_expandView.contentMode =UIViewContentModeScaleAspectFill;
_expandView.clipsToBounds =YES;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if (![keyPathisEqualToString:SunExpandContentOffset]) {
return;
}
[selfscrollViewDidScroll:_scrollView];
}
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
CGFloat offsetY = scrollView.contentOffset.y;
if(offsetY <_expandHeight * -1) {
CGRect currentFrame =_expandView.frame;
currentFrame.origin.y = offsetY;
currentFrame.size.height = -1*offsetY;
_expandView.frame = currentFrame;
NSLog(@"1111");
}
}
- (void)dealloc
{
if (_scrollView) {
[_scrollViewremoveObserver:selfforKeyPath:SunExpandContentOffset];
_scrollView =nil;
}
_expandView =nil;
}
http://download.csdn.net/detail/s_c_w_sun/9400463