2010-06-21 86 views
15

我有一个UIScrollView,我正在应用黑色背景,它在滚动条中混合。我一直在寻找一种方法将滚动条颜色更改为白色,但我无法弄清楚。有没有适当的方法来做到这一点?UIScrollView滚动条颜色?

回答

27

您可以使用 “indicatorStyle” 属性:

[scrollView1 setBackgroundColor:[UIColor blackColor]]; 
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
+3

我可以_永远_记得这个属性名。除此之外,其他一切都称为scrollIndicatorSomething。即使自动完成也不能拯救我从这样一个严重命名的属性。 – Echelon 2012-01-18 20:01:13

+1

您可以使用以下设置为项目中的所有UIScrollViews设置scrollView指标的颜色: [[UIScrollView appearance] setIndicatorStyle:UIScrollViewIndicatorStyleWhite]; – 2015-01-20 11:59:40

+1

UIScrollView.appearance()。indicatorStyle = UIScrollViewIndicatorStyle.White – johndpope 2015-10-08 05:59:18

2

两个UIScrollView的指标是UIScrollView的子视图。那么,我们可以通过 访问UIScrollView的子视图并更改子视图的属性。

1。新增UIScrollViewDelegate

@interface ViewController : UIViewController<UIScrollViewDelegate> 
@end 

2.添加scrollViewDidScroll在实现部分

-(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]]; 
} 

注: - 由于这些指标更新,每次当您滚动 (意味着重置为默认值)。所以,我们把这段代码放在scrollViewDidScroll 的委托方法中。

enter image description here

0
//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() 
    }