我的UITableView上面有一个UIView。当用户向下滚动时,我希望UIView保持在屏幕顶部并让单元格滚动到它上面。 Shazam应用程序的第一页做了我想要的。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (self.tableView.contentOffset.y > 0) {
CGRect newframe = self.publicTopView.frame;
newframe.origin.y = -self.tableView.contentOffset.y;
self.publicTopView.frame = newframe;
}
}
这是我到目前为止所尝试过的,但它并没有做任何事情。感谢
答案 0 :(得分:1)
我会将drag
设置为您的视图,然后将您的单元格设置为清晰(或任何您喜欢的透明度)。这将允许单元格在视图上移动。
答案 1 :(得分:0)
您需要具有透明背景的scrollView。然后添加一个不透明的子视图,稍微向下偏移。这是一个矫枉过正的例子。 TextureView只是让你可以看到背景没有移动。
#import "ViewController.h"
@interface TextureView : UIView
@end
@implementation TextureView
-(void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, rect.size.width, rect.size.height);
CGContextMoveToPoint(context, 0, rect.size.height);
CGContextAddLineToPoint(context, rect.size.width, 0);
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor]CGColor]);
CGContextSetLineWidth(context, 8);
CGContextStrokePath(context);
}
@end
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect scrollViewFrame = self.view.frame;
CGFloat sectionHeight = 300;
int numberSections = 3;
CGFloat clearSpaceAtTop = 300;
CGRect sectionsViewFrame = CGRectMake(0, clearSpaceAtTop, scrollViewFrame.size.width, sectionHeight * numberSections);
CGSize contentSize = CGSizeMake(scrollViewFrame.size.width, CGRectGetMaxY(sectionsViewFrame));
TextureView *backGroundView = [[TextureView alloc]initWithFrame:scrollViewFrame];
backGroundView.backgroundColor = [UIColor blueColor];
[self.view addSubview:backGroundView];
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:scrollViewFrame];
scrollView.contentSize = contentSize;
scrollView.backgroundColor = [UIColor clearColor];
[self.view addSubview:scrollView];
UIView *sectionViewsParent = [[UIView alloc]initWithFrame:sectionsViewFrame];
sectionViewsParent.backgroundColor = [UIColor lightGrayColor];
[scrollView addSubview:sectionViewsParent];
NSArray *colors = @[[UIColor redColor],[UIColor greenColor],[UIColor purpleColor]];
CGFloat spacer = 4;
CGRect colorViewFrame = CGRectMake(0, 0, sectionsViewFrame.size.width, sectionHeight - spacer);
for (UIColor *color in colors){
UIView *sectionView = [[UIView alloc]initWithFrame:colorViewFrame];
sectionView.backgroundColor = color;
[sectionViewsParent addSubview:sectionView];
colorViewFrame.origin.y += sectionHeight;
}
}
@end
答案 2 :(得分:0)
如果调用UIView实例iconView
,则称UITableView实例为tableView
,
如果您的视图控制器是UIViewController。
[self.view addSubview:iconView];
[self.view addSubview:tableView];
//and you need set tableView.background to transparent.
如果你的视图控制器是UITableViewController,请阅读这个问题:
答案 3 :(得分:0)
基本上你所要做的就是使用UIScrollView的contentInset属性
let topInset = topViewHeight.constant //based on your needs
sampleTableView.contentInset = UIEdgeInsets(top: topInset, left: 0, bottom: 0, right: 0)
请参阅我的github示例here