我的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;
}}
这就是我到目前为止尝试过的方法,但它什么也做不了。谢谢
发布于 2015-05-28 10:02:42
我会将tableView.backgroundView设置为您的视图,然后将您的单元格设置为清晰(或您喜欢的任何透明度)。这将允许单元格在视图上移动。
发布于 2015-05-28 09:33:08
你需要有一个透明背景的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发布于 2015-05-28 10:25:56
如果UIView实例称为iconView,则UITableView实例称为tableView,如果您的视图控制器为UIViewController。
[self.view addSubview:iconView];
[self.view addSubview:tableView];
//and you need set tableView.background to transparent.如果您的视图控制器是UITableViewController,请阅读以下问题:
https://stackoverflow.com/questions/30495711
复制相似问题