Giter Club home page Giter Club logo

asjcoloredscrollindicators's Introduction

ASJColoredScrollIndicators

The stock UIScrollView provides limited options to customize its scroll indicators, with a sole enum UIScrollViewIndicatorStyle that may prove lacking. It makes sense sometimes to paint the indicators as per the app's theme to give it a little extra. This small utility provides a UIScrollView category with a lightweight solution to apply a color to the scroll indicators.

After coming across JTSScrollIndicator and looking at its implementation, I felt there could be an easier way to do this. My implementation gets a hold of the scroll indicator objects themselves and paints them to the desired color. I feel this is a much more simple and direct approach.

Screenshot

Installation

CocoaPods is the preferred way to install this library. Add this command to your Podfile:

pod 'ASJColoredScrollIndicators'

Usage

Import ASJColoredScrollIndicators.h and set this property to any UIScrollView:

@property (nullable, strong, nonatomic) UIColor *scrollIndicatorColor UI_APPEARANCE_SELECTOR;

Paints both the horizontal and vertical scroll indicators to the desired color.

You may also set the property via UIAppearance to set the color globally:

[UIScrollView appearance].scrollIndicatorColor = [UIColor blueColor];

Since this is a category on UIScrollView, it works with all subclasses:

  • UITableView
  • UICollectionView
  • UITextView

How it works

UIScrollView's scroll indicators are not publicly available. Looking at its subviews, we find two UIImageViews that appear to be the indicators. Each has either their width or height 2.5 pts. Interestingly, no matter whether your view scrolls in both directions, the vertical and horizontal indicator objects are present.

Indicators

So it would seem that modifying these UIImageViews would do the job. But no! What I found is that these objects are changed when the scrolling is active; their memory addresses were different. This happens every time. You could check the indicators in scrollViewDidScroll: or scrollViewWillBeginDragging, the addresses there are the same, but not the same when the UIScrollView initially loaded.

When this was clear, the rest was pretty simple. I tried to change the background color of those UIImageViews but the effect was not nice; there still was some transparency. I ended up removing the image's color information and setting a background color to the UIImageView with some rounding to shape it look like a default indicator. Voilà!

This color change is done exactly once, when the UIScrollView is about to be scrolled for the very first time.

Credits

  • To Shashank Pali for his perseverence to unite this functionality under a single category.

To-do

  • Add UICollectionView and UITextView.
  • Find a way to combine the four in one class.

License

ASJColoredScrollIndicators is available under the MIT license. See the LICENSE file for more info.

asjcoloredscrollindicators's People

Contributors

readmecritic avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.