Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable flashing scroll bar of a UITableViewController?

I have a UITableViewController, and I'd like to make it not flash the vertical scroll bar when I go back from a push action segue on one of it's cells (popping the view controller and going back to the UITableViewController).

It seems that, if the table has many rows (mine has around 20 with 60 points height each, so bigger than the screen), when I go back, it always flashes the vertical scroll bar once to show where it is in the table. However, I don't want that to happen, but I do want to keep the scrollbar around so it shows when the user scrolls. Therefore, disabling it completely is not an option.

Is this default behavior and can I disable it temporarily?

like image 919
swiftcode Avatar asked Dec 05 '22 10:12

swiftcode


2 Answers

There is a simpler solution that doesn't require avoiding using a UITableViewController subclass.

You can override viewDidAppear: as stated by http://stackoverflow.com/users/2445863/yonosoytu, but there is no need to refrain from calling [super viewDidAppear:animated]. Simply disable the vertical scrolling indicator before doing so, and then enable it back afterwards.

- (void)viewDidAppear:(BOOL)animated {
    self.tableView.showsVerticalScrollIndicator = NO;
    [super viewDidAppear:animated];
    self.tableView.showsVerticalScrollIndicator = YES;
}

If you're using Interface Builder, you can disable the Shows Vertical Indicator option on the tableView for your UIViewController and enable it in code as shown above.

like image 111
Cezar Avatar answered Jan 31 '23 09:01

Cezar


To get Cezar's answer to work for iOS10 I had to include a (sizeable) delay before re-enabling the scroll indicator. This looks a bit strange if someone tries to scroll before the second is up, so you can re-enable the scroll indicator as soon as someone scrolls.

override func viewDidAppear(_ animated: Bool) {
    tableView.showsVerticalScrollIndicator = false
    super.viewDidAppear(animated)
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        self.tableView.showsVerticalScrollIndicator = true
    }
}

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if !tableView.showsVerticalScrollIndicator {
        tableView.showsVerticalScrollIndicator = true
    }
}

Actually, on thinking about it, you don't even need the delay, just do this:

override func viewDidAppear(_ animated: Bool) {
    tableView.showsVerticalScrollIndicator = false
    super.viewDidAppear(animated)
}

override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if !tableView.showsVerticalScrollIndicator {
        tableView.showsVerticalScrollIndicator = true
    }
}
like image 42
iOS-Dean Avatar answered Jan 31 '23 08:01

iOS-Dean