How to change UIScrollView Indicator color?

萝らか妹 提交于 2021-01-27 21:06:25

问题


When I use UIColor.blue its changing, but when I try to apply RGB, its not reflected in scrollview indicator.

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let verticalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 1)] as! UIImageView)
    verticalIndicator.backgroundColor = UIColor(red: 211/255, green: 138/255, blue: 252/255, alpha: 1)
//        verticalIndicator.backgroundColor = UIColor.blue

}

回答1:


I think problem is here

verticalIndicator.backgroundColor = UIColor(red: 211/255, green: 138/255, blue: 252/255, alpha: 1)

Change to

verticalIndicator.backgroundColor = UIColor(red: 211/255.0, green: 138/255.0, blue: 252/255.0, alpha: 1).

The result of division 211/255 is type of integer so it return only 0 or 1 (in this case i think it is 0).




回答2:


When using RGB values set out UIColor like this

self.view.backgroundColor = UIColor.init(red: <#T##CGFloat#>, green: <#T##CGFloat#>, blue: <#T##CGFloat#>, alpha: <#T##CGFloat#>)



回答3:


Both UIScrollView indicator are sub view of UIScrollView. So, we can access subview of UIScrollView and change the property of subview.

1 .Add UIScrollViewDelegate

@interface ViewController : UIViewController<UIScrollViewDelegate>
@end

2. Add scrollViewDidScroll in implementation section

-(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 these indicator update every time when you scroll (means reset to default). SO, we put this code in scrollViewDidScroll delegate method.

For Swift

Add UIScrollView Delegate.

func scrollViewDidScroll(scrollView: UIScrollView){
   let verticalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 1)] as! UIImageView)
   verticalIndicator.backgroundColor = UIColor.greenColor()

   let horizontalIndicator: UIImageView = (scrollView.subviews[(scrollView.subviews.count - 2)] as! UIImageView)
   horizontalIndicator.backgroundColor = UIColor.blueColor()
}



回答4:


You should also add this line in order to achieve the exact color you want:

verticalIndicator.image = nil


来源:https://stackoverflow.com/questions/40484070/how-to-change-uiscrollview-indicator-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!