2010-06-21 83 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() 
    }