http://blog.163.com/it__man/blog/static/13719990420131183483729/
程序中经常遇到类似这样的需求:tableview随着手指的滑动,区域自动变化,推走tableview上方的其它控件(比如navgationbar)。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGRect frame = scrollView.frame;
float scrollViewY = frame.origin.y;
frame.origin.y = frame.origin.y - scrollView.contentOffset.y;
NSLog(@"scrollView.contentOffset.y = %.2f, frame.origin.y = %.2f, scrollView.bounds.origin.y = %.2f", scrollView.contentOffset.y, frame.origin.y,scrollView.bounds.origin.y);
CGFloat height = 40;//height of top control on tableview
CGRect aOriginRect = scrollView.frame;
aOriginRect.origin.y = aOriginRect.origin.y - scrollView.contentOffset.y;
if (aOriginRect.origin.y <= height && aOriginRect.origin.y >= 0){
CGRect aOriginBounds = scrollView.bounds;
aOriginBounds.origin = CGPointMake(0, 0);
scrollView.bounds = aOriginBounds;
}
else if (aOriginRect.origin.y > height){
aOriginRect.origin.y = height;
}
else{
aOriginRect.origin.y = 0;
}
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
aOriginRect.size.height = screenSize.height - aOriginRect.origin.y - 20 - 40; //20:statusBarHeight 40:bottom bar height
scrollView.frame = aOriginRect;
self.titleView.frame = CGRectMake(self.titleView.frame.origin.x, scrollView.frame.origin.y - height, self.titleView.frame.size.width, self.titleView.frame.size.height);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGRect frame = scrollView.frame;
float scrollViewY = frame.origin.y;
frame.origin.y = frame.origin.y - scrollView.contentOffset.y;
NSLog(@"scrollView.contentOffset.y = %.2f, frame.origin.y = %.2f, scrollView.bounds.origin.y = %.2f", scrollView.contentOffset.y, frame.origin.y,scrollView.bounds.origin.y);
CGFloat height = 40;//height of top control on tableview
CGRect aOriginRect = scrollView.frame;
aOriginRect.origin.y = aOriginRect.origin.y - scrollView.contentOffset.y;
if (aOriginRect.origin.y <= height && aOriginRect.origin.y >= 0){
CGRect aOriginBounds = scrollView.bounds;
aOriginBounds.origin = CGPointMake(0, 0);
scrollView.bounds = aOriginBounds;
}
else if (aOriginRect.origin.y > height){
aOriginRect.origin.y = height;
}
else{
aOriginRect.origin.y = 0;
}
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
aOriginRect.size.height = screenSize.height - aOriginRect.origin.y - 20 - 40; //20:statusBarHeight 40:bottom bar height
scrollView.frame = aOriginRect;
self.titleView.frame = CGRectMake(self.titleView.frame.origin.x, scrollView.frame.origin.y - height, self.titleView.frame.size.width, self.titleView.frame.size.height);
}