How can I always show the UIScrollBar indicator? - swift

When a UIViewController with a UIScrollView is visible on screen, I want to show the UIScrollView scrollbar indicator.
Is there a way to show the indicator?

You can show the scrollbar momentarily with .flashScrollIndicators when the view controller has been added to the view hierarchy and is potentially visible to the user.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
scrollView.flashScrollIndicators()
}

Related

Scroll UITableView up before popping UIViewController

I have a custom pop transition and it depends on the UITableView being scrolled to the top before performing the transition. The second I tap the back button of my UIViewController I want to scroll the UITableView up which will then have the correct state for my transition.
I already tried to scroll the UITableView up before the view will get dismissed:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
}
This however failed to scroll at all. How can I move the UITableView to the top before the UIViewController gets dismissed?
Overriding willMove(toParent:) and checking for parent == nil should allow you to perform the animation at the required time. See this response on intercepting nav back button.

How to stop video in UITableViewCell when new screen is pushed

I do have tab bar controller with few tabs - one of the tabs looks like this:
I have a table view filled with cells
Some of the cells contains AVPlayer and AVPlayerLayer
I'm using a delegate method tableView:didEndDisplayingCell:forRowAtIndexPath to stop videos that are scrolled out of the visible area
What I need:
When I push a new controller, I need to stop video that is currently playing
When I tap on different tab in tab bar, I need to stop video as well
I tried to implement func willMove(toWindow newWindow: UIWindow?), but it has one annoying side-effect: I have to manually setup dismiss gesture for all the pushed screens.
Any alternative ideas about how to solve that problem? Thanks guys
You can do this in viewWillDisappear function of a UIViewController which has the video cells.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
for cell in tableView.visibleCells {
(cell as VideoCell).stopVideo()
}
}

bottom tabbar disappears and leave black when pushed from UITableViewController to UIViewController

In my use case, I want to hide the bottom tabbar when navigating away from UITabbarController.
I was using
let vc = storyboard?.instantiateViewController(withIdentifier: tableData[indexPath.row]["vcIdentifier"]!)
self.hidesBottomBarWhenPushed = true
self.show(vc!, sender: self)
It sorta works, because the pushed view controller doesn't have tabbar at bottom. However, as soon as I click on navigate, the bottom tabbar of the "sender" view controller vanishes and leaves black area.
Please let me know if you need to have more information about anything. Thanks a lot in advance!
If the pushed view controller doesn't have a tab bar at the bottom, you can add this lifecycle of view controller codes.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tabBarController?.tabBar.isHidden = true
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
tabBarController?.tabBar.isHidden = false
}
you can use segue if you want to hide tabbar while going to the next screen. it will automatically hide it.

Swift - Selected tab bar index not triggering scroll to top

I have a tab bar with five items, and I am trying to add a functionality to scroll to the top when the user taps the tab bar item again. Added the UITabBarControllerDelegate to the views where I want to trigger the event and also created a function to determine the selected tab bar index.
When I open the app, index 0 is auto-selected and works perfectly. The view auto scrolls to the top when I scroll down and tap the tab bar index. The problem occurs when I go to index 1 and trigger the scroll there. It somehow completely removes the auto-scroll from my first tab bar item.
Selecting other tab bar items without the auto scroll does not affect index 0 at all.
Home (index 0)
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let tabBarIndex = tabBarController.selectedIndex
if tabBarIndex == 0 {
self.collectionView?.setContentOffset(CGPoint(x: 0, y: -10), animated: true)
}
}
Users (index 1)
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let tabBarIndex = tabBarController.selectedIndex
if tabBarIndex == 1 {
self.tableView?.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
}
}
Anything with a delegate property can only have one delegate assigned to it at any given time. What ever set the delegate most recently will receive the next delegate method call.
In your case you can probably reset the tab controller's delegate to self in each view controller's viewDidAppear method since you want the currently visible view controller to be the current tab controller delegate.
Add the following to each view controller that needs to be the tab controller's delegate:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.tabBarController?.delegate = self
}

How to show and hide navigationBar?

I have two UITableViewController, and in each of them NavigationBar is visible; then I have a simple UIViewController. So I'd like to make NavigationBar invisible only in the third view. I tried
self.navigationController?.navigationBarHidden = true
but this make navigationBar invisibile in every view, after I leave the third one.
I also tried
override func prefersStatusBarHidden() -> Bool {
return true
}
This is my application scheme: only in "DettaglioController" I'd like to make navigationBar invisible.
Any ideas to solve?
Its just one line of code....
navigationController?.setNavigationBarHidden(true, animated: true)
In the ViewControllers viewWillAppear you can hide the NavigationBar like this, and in its viewWillDisappear you can show it back again