HOW TO CUSTOMISE SCROLL BAR IN UITABLEVIEW IN SWIFT 3

HOW TO CUSTOMISE SCROLL BAR IN UITABLEVIEW IN SWIFT 3

In this tutorial you will learn how to make scrollbar visible in a flash and keep the scrollbar displaying  in the tableview even when the user isn’t scrolling the table.

DISPLAYING SCROLLBAR IN A FLASH

First, we will create an outlet of the UITableView.

@IBOutlet var tableview: UITableView!

We have created an outlet named as tableview in our view controller. Lets proceed with displaying the tableview’s scrollbar in a flash using Swift 3.

tableview.flashScrollIndicators()

The above code will make the scrollbar visible to the user like a flash i.e it will get displayed and then after a fraction of time it will go invisible until unless the user taps on the tableView to scroll the cells.

Now what if the you want the scrollbar to be visible all the time even when the user is not interacting? Take a chill pill and scroll down to get the solution.

 

DISPLAYING THE SCROLLBAR PERMANENTLY IN UITABLEVIEW

It is possible to keep the scrollbar visible irrespective of the interaction by the user. To make it happen we’ll require the use of Timer in our view controller.

Declare a variable of type Timer in the view controller:

var timerForShowScrollIndicator: Timer?

In ViewDidAppear, add the following code:

override func viewDidAppear(_ animated: Bool) {

super.viewDidAppear(animated)

startTimerForShowScrollIndicator()

}

and in add the following code in viewDidDisappear:

override func viewDidDisappear(_ animated: Bool) {

super.viewDidDisappear(animated)

stopTimerForShowScrollIndicator()

}

Lets define the functions we have called in viewDidAppear() and viewDidDisappear(). Function in viewDidAppear is to start the timer to show the scrollbar in tableview and function in viewDidDisappear is to stop the timer.

//TO START THE TIMER

func startTimerForShowScrollIndicator() {

        self.timerForShowScrollIndicator = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(self.showScrollIndicators), userInfo: nil, repeats: true)

    }

//TO STOP THE TIMER

func stopTimerForShowScrollIndicator() {

self.timerForShowScrollIndicator?.invalidate()

self.timerForShowScrollIndicator = nil

}

Now, lets define the last function i.e. self.showScrollIndicators()

func self.showScrollIndicators() {

UIView.animate(withDuration: 0.0001) {

self.view_main.flashScrollIndicators()

}

}

Compile and run the project and there you have the scrollbar visible all the time in the tableview.

Note: You can use this code in UIView, UITableView, UICollectionView, UIScrollView etc i.e.wherever a scrollbar is displayed. So go ahead and enjoy swift. 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s