简体   繁体   中英

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

}

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

使用 RGB 值时,像这样设置 UIColor

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

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

您还应该添加此行以获得所需的确切颜色:

verticalIndicator.image = nil

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM