Karthikeyan Bose
Karthikeyan Bose

Reputation: 1274

How to change UIScrollView Indicator color?

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

}

Upvotes: 2

Views: 9702

Answers (5)

hamit seyrek
hamit seyrek

Reputation: 1

you can use this code for changing scrollIndicator of tableView

    if let indicatorView = self.tableView.subviews.first(where: { $0.bounds.size.width <= 10 }) {
        indicatorView.backgroundColor = UIColor(hexString: "5A2A7F")
    }

Upvotes: 0

Alchi
Alchi

Reputation: 849

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

verticalIndicator.image = nil

Upvotes: 0

Milan S Dodiya
Milan S Dodiya

Reputation: 11

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

Upvotes: 1

user6193496
user6193496

Reputation:

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#>)

Upvotes: 1

nynohu
nynohu

Reputation: 1658

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).

Upvotes: 6

Related Questions