代码之家  ›  专栏  ›  技术社区  ›  Kyle

UIScrollView滚动条颜色?

  •  15
  • Kyle  · 技术社区  · 15 年前

    我有一个UIScrollView,我正在应用一个黑色的背景,它混合在滚动条中。我一直在寻找一种方法来改变滚动条的颜色为白色,但我想不出来。有合适的方法吗?

    3 回复  |  直到 15 年前
        1
  •  27
  •   mav    15 年前

    [scrollView1 setBackgroundColor:[UIColor blackColor]];
    scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    
        2
  •  2
  •   Vineet Choudhary    10 年前

    两个UIScrollView指示器都是UIScrollView的子视图。所以,我们可以 访问UIScrollView的子视图并更改子视图的属性。

    1.添加UIScrollViewDelegate

    @interface ViewController : UIViewController<UIScrollViewDelegate>
    @end
    

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView1
    {
        //get refrence of vertical indicator
        UIImageView *verticalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-1)]);
        //set color to vertical indicator
        [verticalIndicator setBackgroundColor:[UIColor redColor]];
    
    
        //get refrence of horizontal indicator
        UIImageView *horizontalIndicator = ((UIImageView *)[scrollView.subviews objectAtIndex:(scrollView.subviews.count-2)]);
        //set color to horizontal indicator
        [horizontalIndicator setBackgroundColor:[UIColor blueColor]];
    }
    

    Note:- Because 每次滚动时,这些指示器都会更新 (指重置为默认值)。所以,我们把这个代码放在scrollViewDidScroll中

    enter image description here

        3
  •  0
  •   Alvin George    9 年前
    //ScrollView Deleagate
        func scrollViewDidScroll(scrollView: UIScrollView){
            let verticalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 1)] as! UIImageView)
            verticalIndicator.backgroundColor = UIColor.redColor()
    
    
            let horizontalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 2)] as! UIImageView)
            horizontalIndicator.backgroundColor = UIColor.blueColor()
        }