【问题标题】:IOS - Have UITableViewCells scroll over UIView (like the shazam app)IOS - 让 UITableViewCells 在 UIView 上滚动(如 shazam 应用程序)
【发布时间】:2015-08-10 07:42:14
【问题描述】:

我的 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;
}

}

这是我迄今为止尝试过的,但它没有做任何事情。谢谢

【问题讨论】:

    标签: ios xcode uitableview uiview


    【解决方案1】:

    我会将tableView.backgroundView 设置为您的视图,然后将您的单元格设置为清晰(或任何您喜欢的透明度)。这将允许单元格在视图上移动。

    【讨论】:

      【解决方案2】:

      你需要一个带有透明背景的滚动视图。然后向它添加一个不透明的子视图并向下偏移一点。这是一个矫枉过正的例子。 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
      

      【讨论】:

        【解决方案3】:

        如果UIView实例被称为iconView,UITableView实例被称为tableView, 如果你的视图控制器是 UIViewController。

        [self.view addSubview:iconView];
        [self.view addSubview:tableView];
        //and you need set tableView.background to transparent.
        

        如果您的视图控制器是 UITableViewController,请阅读以下问题:

        UITableViewController Background Image

        【讨论】:

          【解决方案4】:

          基本上你所要做的就是使用 UIScrollView 的 contentInset 属性

          let topInset = topViewHeight.constant //based on your needs
          sampleTableView.contentInset = UIEdgeInsets(top: topInset, left: 0, bottom: 0, right: 0)
          

          查看我的 github 示例 here

          【讨论】:

            猜你喜欢
            • 2022-06-16
            • 1970-01-01
            • 1970-01-01
            • 2017-07-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多