如何更改UIScrollView指示灯颜色?

时间:2016-11-08 10:07:12

标签: ios swift uiscrollview swift3 uicolor

当我使用 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

}

4 个答案:

答案 0 :(得分:5)

我认为问题在这里

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

  

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

对于Swift

  

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

答案 3 :(得分:0)

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

verticalIndicator.image = nil