iOS Changing Uiscrollview Scrollbar Color to Different Colors

How to change UIScrollView Indicator color?

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

ios Changing UIScrollView scrollbar color to different colors

Unfortunately you can't, of course you can always roll your own. These are your options:

UIScrollViewIndicatorStyleDefault:

The default style of scroll indicator, which is black with a white border. This style is good against any content background.

UIScrollViewIndicatorStyleBlack:

A style of indicator which is black and smaller than the default style. This style is good against a white content background.

UIScrollViewIndicatorStyleWhite:

A style of indicator is white and smaller than the default style. This style is good against a black content background.

How do I change the colour of scroll bar Indicator in UICollectionView apart from Default,Black or White?

You can use delegate method : 'scrollViewDidScroll'
and add

//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]];

How to change colour of scrollbar when calling flashScrollIndicator() in Swift 3

You just need to put those two pieces together. If you add the color-changing code to viewDidAppear, it should work just like it does in scrollViewDidScroll.

override func viewDidAppear(_ animated: Bool) {
let verticalIndicator = tableView.subviews.last as? UIImageView
verticalIndicator?.backgroundColor = UIColor.red
self.tableView.flashScrollIndicators()
}

While this is possible, I don't recommend it for a few reasons:

  1. It's fragile. Since this isn't a readily available public API, there's no guarantee this will work in future iOS versions.

  2. It's probably a bad design choice. Do you really want to point the user's attention to a differently colored scroll bar? Most users familiar with other apps are used to the default look of the scroll bars, so it communicates their position in the view without much extra attention. Also it doesn't look very nice when I just tried it.

The alternative solution is to simply set the scroll indicator style to one of the available options (default, white, or black).

tableView.indicatorStyle = .default

Change width and colour of scroll bar in UITableView, iphone

You can set only style of scroll indicators:

The style of the scroll indicators.

@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle

Styles:

Scroll Indicator Style
The style of the scroll indicators. You use these constants to set the value of the indicatorStyle style.

typedef enum {
UIScrollViewIndicatorStyleDefault,
UIScrollViewIndicatorStyleBlack,
UIScrollViewIndicatorStyleWhite
} UIScrollViewIndicatorStyle;

For example:

tableView.indicatorStyle = UIScrollViewIndicatorStyleBlack;

How do I change the background color of UIScrollView in Swift?

When you are calling

for k in 0 ... 5 {
scrollView.backgroundColor = backgroundColorArray[k]
}

you are looping through each color in backgroundColorArray and updating the value. Because .purple is the last element in your backgroundColorArray, your scroll view, at the end of this for loop, is being set to purple. Your for loop is essentially doing the same thing as scrollView.backgroundColor = backgroundColorArray[5].

Now, what I suggest you do is implement UIScrollViewDelegate methods. You could implement scrollViewDidScroll(scrollView:). This is called every time the scroll view's content offset changes. What you could then do is set a switch statement that checks the content offset of your scroll view, and assigns the color accordingly:

extension MyViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//Assuming your pages are situated horizontally from one another
let page = scrollView.contentOffset.x / view.frame.width
if page < 1 {
scrollView.backgroundColor = backgroundColorArray[0]
} else if page > 1 && page < 2 {
scrollView.backgroundColor = backgroundColorArray[1]
} else if page > 2 && page < 3 {
scrollView.backgroundColor = backgroundColorArray[2]
} else if page > 3 && page < 4 {
scrollView.backgroundColor = backgroundColorArray[3]
} else if page > 4 && page < 5 {
scrollView.backgroundColor = backgroundColorArray[4]
} else if page > 5 && page < 6 {
scrollView.backgroundColor = backgroundColorArray[5]
}
}
}

You could also interpolate between colors depending on your scroll view's content offset. Here is a UIColor extension I use to do this:

extension UIColor {
static func interpolate(from fromColor: UIColor, to toColor: UIColor, with progress: CGFloat) -> UIColor {
let fromComponents = fromColor.components
let toComponents = toColor.components

let r = (1 - progress) * fromComponents.r + progress * toComponents.r
let g = (1 - progress) * fromComponents.g + progress * toComponents.g
let b = (1 - progress) * fromComponents.b + progress * toComponents.b
let a = (1 - progress) * fromComponents.a + progress * toComponents.a

return UIColor(red: r, green: g, blue: b, alpha: a)
}
}

However I highly suggest you take a look at UIPageViewController. It sounds like it does more of what you are trying to manually do yourself. Check out this question I asked recently: Pan (not swipe) between view controllers

How to change the UIText view vertical scrollbar indicator color?

Use following code to update indicator color of UIScrollView

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

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

}

Custom UIScrollView scrollbar color?

You don't say, but since you're using UIKit, I assume you'd be developing for iOS.

scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;

Replace UIScrollViewIndicatorStyleWhite to anything from this typedef.



Related Topics



Leave a reply



Submit