繁体   English   中英

如何更改 UIScrollView 指示器颜色?

[英]How to change UIScrollView Indicator color?

当我使用UIColor.blue 时,它会发生变化,但是当我尝试应用 RGB 时,它不会反映在滚动视图指示器中。

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

}

我认为问题就在这里

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

更改为

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

除法 211/255 的结果是整数类型,所以它只返回 0 或 1(在这种情况下我认为它是 0)。

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

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

两个 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 委托方法中。

对于斯威夫特

添加 UIScrollView 委托。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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