UIScrollView - showing the scroll bar

前端 未结 10 1746
-上瘾入骨i
-上瘾入骨i 2020-11-27 20:32

Possibly a simple one!

Does anyone know how to get the scroll bar of a UIScrollView to constantly show?

It displays when the user is scrolling, so they can s

10条回答
  •  一生所求
    2020-11-27 21:10

    ScrollBar that functions just like the iOS built in one, but you can mess with the color and width.

    -(void)persistantScrollBar
    {
        [persistantScrollBar removeFromSuperview];
        [self.collectionView setNeedsLayout];
        [self.collectionView layoutIfNeeded];
    
        if (self.collectionView.contentSize.height > self.collectionView.frame.size.height + 10)
        {
            persistantScrollBar = [[UIView alloc] initWithFrame:(CGRectMake(self.view.frame.size.width - 10, self.collectionView.frame.origin.y, 5, (self.collectionView.frame.size.height /self.collectionView.contentSize.height) * self.collectionView.frame.size.height))];
            persistantScrollBar.backgroundColor = [UIColor colorWithRed:207/255.f green:207/255.f blue:207/255.f alpha:0.5f];
            persistantScrollBar.layer.cornerRadius = persistantScrollBar.frame.size.width/2;
            persistantScrollBar.layer.zPosition = 0;
            [self.view addSubview:persistantScrollBar];
        }
    }
    
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        CGRect rect = persistantScrollBar.frame;
        rect.origin.y =  scrollView.frame.origin.y + (scrollView.contentOffset.y *(self.collectionView.frame.size.height/self.collectionView.contentSize.height));
        rect.size.height = (self.collectionView.frame.size.height /self.collectionView.contentSize.height) * self.collectionView.frame.size.height;
        if ( scrollView.contentOffset.y <= 0 )
        {
            rect.origin.y = scrollView.frame.origin.y;
            rect.size.height = rect.size.height + (scrollView.contentOffset.y);
        }
        else if (scrollView.contentOffset.y + scrollView.frame.size.height >= scrollView.contentSize.height)
        {
            rect.size.height = rect.size.height - ((scrollView.contentOffset.y + scrollView.frame.size.height) - scrollView.contentSize.height);
            rect.origin.y =  (self.collectionView.frame.origin.y + self.collectionView.frame.size.height - 5) - rect.size.height;
        }
    
        persistantScrollBar.frame = rect;
    }
    

提交回复
热议问题