I need to scroll to the top when I tapped second time to the first BarButtonItem (Home). There are a lot of answers but none of them don't work for me. It's incredible but it is.
I have UITabBarControllerDelegate in my class, self.tabBarController?.delegate = self in viewDidLoad() and this in my TableViewController:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if self.tabBarController?.selectedIndex == 0 {
self.tableView.setContentOffset(CGPoint(x: 0, y: -60), animated: true)
}
}
It works everytime I tapped to the Home (index = 0), but I need to do this when I'm already on the Home screen (standart function for all social media to scroll to the top Feed by tapping Home). And how to set Y coordinate if I have a NavigationBar above?
Thanks
All you have to do is check if selected viewController is your view controller that has tableView you need to scroll to top. Now I'd recommend using scroll to instead of setting content offset because that may vary based on the device and other logic (like large title for example)
Considering you have your custom tab bar controller that has all the tabs references you could do something like this:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if viewController == myViewController {
myViewController.tableView.scrollToRow(
at: IndexPath(row: 0, section: 0),
at: .top,
animated: true)
}
}
}
Related
I have a Swift application with 4 bottom tabs. In the home tab I have a video running. In the 4th tab I have a favorites list.
When the user changes tabs, if he's on the home screen, the video should stop. Also, when the user taps on the 4th tab, the favorites list should update so that the user can see the recent additions.
I have the following on the home tab view controller:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print("TAB CHANGED")
if let jwp = jwPlayer {
jwp.stop()
}
}
and this on the favorites tab view controller:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let tabBarIndex = tabBarController.selectedIndex
if tabBarIndex == 3 {
let userId = UserDefaults.standard.integer(forKey: "userId")
favoritesVC.updateData(with: userId)
}
}
Both conform to UITabBarControllerDelegate. Both include:
self.tabBarController?.delegate = self
When I launch the app, the video starts playing, I tap on any tab and the video stops. No matter which tab I tap on I see the message "TAB CHANGED". But as soon as I tap on the favorites tab and I move to another, I stop seeing the "TAB CHANGED" message. If I then move to the home screen and play the video and then move to a different tab, the video no longer stops.
The didSelect on the 4th tab is cancelling the didSelect on the 1st tab.
How can I get both of them to work? I have placed them on both view controllers because on the first one I need to reference the video and on the 2nd one I need to reference the list view controller (the 4th view controller actually has two top tabs which switch between a favorites vc and a downloads vc).
UPDATE:
I moved the delegate to the TabBarController subclass. I added the following:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let tabBarIndex = tabBarController.selectedIndex
let hvc = HomeViewController()
if tabBarIndex != 0 {
hvc.stopPlayer()
}
}
I added
delegate = self
and
class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
In the HomeViewController I added this:
func stopPlayer() {
print("TAB CHANGED")
if let jwp = jwPlayer {
jwp.stop()
}
}
however when I change tabs, jwPlayer is always nil.
It seems like you are handling the UITabBarControllerDelegate calbacks in multiple places and for that to happen, you are also changing following multiple times -
self.tabBarController?.delegate = self
Here's what you should do -
Handle UITabBarControllerDelegate in one place.
Dispatch the necessary work calls from there to relevant screens.
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if tabBarIndex == 0 {
homeVC.startPlayer()
}
else {
homeVC.stopPlayer()
if tabBarIndex == 3 {
let userId = UserDefaults.standard.integer(forKey: "userId")
favoritesVC.updateData(with: userId)
}
}
}
This can be handled anywhere, inside your TabBarController subclass if you have one, or any other object that is guaranteed to exist for the application lifetime.
TabBarController’s delegate does not have to be either of the view controllers. It could be some other object (not even a view controller) that could hold weak references to both view controllers, for example, or post a notification etc
So I have a UITabBarController with two View Controllers Embedded into it. I implemented the did select tab bar method where when the user selects a tab, it passes a value into that controller. However when the tabBarController loads for the first time, the did select method is not called even though I have
self.selectedIndex = 0
Which selects the first index. Basically I am just trying to automatically select the first tab Bar Item when the view loads, and have it call the didSelectTabBarItem method
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
}
The question is similar to this one.
tabBarController didSelect does not get called
optional func tabBarController(_ tabBarController: UITabBarController,
didSelect viewController: UIViewController)
In iOS v3.0 and later, the tab bar controller calls this method regardless of whether the selected view controller changed. In addition, it is called only in response to user taps in the tab bar and is not called when your code changes the tab bar contents programmatically.
Copy this in your code
class HomeTabBarVC: UITabBarController {
var isIpad = false
let button = UIButton.init(type: .custom)
var index = 0
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
APPDELEGATE.tabBar = self
}
}
extension HomeTabBarVC : UITabBarControllerDelegate {
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("the last selected index is : \(selectedIndex)")
APPDELEGATE.tabBarLastSelectedIndex = selectedIndex
print("the current selected index is : \(String(describing: tabBar.items?.index(of: item)))")
APPDELEGATE.tabBarCurrentSelectedIndex = tabBar.items?.index(of: item) ?? 0
}
}
In appdelegate declare this variable,
var tabBar : UITabBarController?
I have a tableview which has load more. I'd like to add scroll to top function when user press tabbar item twice like twitter, instagram.
This is my code when user tap twice to tab bar item.
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if previousController == viewController {
if let navVC = viewController as? UINavigationController, let vc = navVC.viewControllers.first as? AssistantMainViewController {
if vc.isViewLoaded && (vc.view.window != nil) {
// viewController is visible
vc.tableView.setContentOffset(CGPoint(x: 0, y: -173), animated: true)
}
}
}
previousController = viewController
}
It works fine when tableview first load. The problem is when I load more cell to my tableview and scroll to top, tableview not scrolls to top it stuck in middle of somewhere. Other weird thing is if I scroll manually to top after load more, tab bar item scroll tableview properly until load more cells.
In load more action basicly app get more content from server and add them to array and call tableview.reloadData()
adviceDataSource.loadMoreContent(beforeDay: beforeDate, success: {feedCount in
if feedCount == 0 {
self.didGetLastPage = true
}
self.tableView.reloadData()
self.tableView.layoutSubviews()
self.isRunningRequest = false
}, error: { (errorString, statusCode) in
self.isRunningRequest = false
}) { (MoyaError) in
self.isRunningRequest = false
}
Probably the problem is when I load more cell tableview doesn't know its new content size.By the way when tap status bar tableview scroll to top perfectly. If you know the func that called when user tap status bar, it would be perfect for me.
I've been working and searching on it 2 days, any help?
Thank you!
call UITabBarControllerDelegate in your viewcontroller tableView class.
in viewdidload Method call
self.tabBarController?.delegate = self
implement tab bar didselect in your viewcontroller class
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let tabBarIndex = tabBarController.selectedIndex
if tabBarIndex == 0 {
self.tableView.setContentOffset(CGPoint.zero, animated: true)
}
}
Replace tableView.layoutSubviews() with tableView.layoutIfNeeded(), it layout the tableview immediately.
And use the tabbarcontrollerdelegate:
The viewcontroller(which owns the tableview) will listen to it. and use this method
self.primaryTableView.scrollToRow(at: IndexPath, at: UITableViewScrollPosition, animated: Bool)
I'm trying to make tab bar with sliding (or may be swipe is right word?) effect of changing ViewControllers.
I create two ViewControllers with TableView on whole screen, but with restrictions - top edge of table not overlap top layout guide.
I link this ViewControllers with TabBarController and when I use default animation - it's OK, work fine. But I want sliding animation and do something like (swift3):
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
animateSliding(fromController: selectedViewController, toController: viewController)
return true
}
func animateSliding(fromController: UIViewController?, toController: UIViewController?) {
let fromView: UIView = fromController!.view;
let toView: UIView = toController!.view;
fromView.superview?.addSubview(toView);
toView.frame.origin.x = UIScreen.main.bounds.size.width;
UIView.animate(withDuration: 0.3,
animations: {
toView.frame.origin.x = 0;
fromView.frame.origin.x -= UIScreen.main.bounds.size.width;
},
completion: nil);
}
(it's not complete animation, just sample)
Now I have animation I wanted but second's ViewController's table overlap top guide when appear (when slide over first viewController). If I change position ViewControllers in TabBar (first became second and second became first) situation change - not first controller's table overlap top guide (when appear)
Have you tried XLPagerTabStrip? I believe that it is pretty similar to what you want to achive.
If not, you can base your code in that implementation.
I want to scroll to the top when i double tap on any BarButtonItem.
I saw a lot of answers on stackOverflow but none of them worked for me.
Maybe I'm using it wrong? Where do i put the code in the AppDelegate or the TableViewControllers i want to add this functionality specifically?
anyway, I'm using Swift 2.3 and Xcode 8 and would love to get some help.
Thank you.
Do you know about scrollsToTop? I think it is what you need. Description from iOS SDK related to scrollsToTop property in UIScrollView:
When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its scrollsToTop property is YES, its delegate does not return NO from shouldScrollViewScrollToTop, and it is not already at the top.
// On iPhone, we execute this gesture only if there's one on-screen scroll view with scrollsToTop == YES. If more than one is found, none will be scrolled.
At first setup UITabBarControllerDelegate delegate. Delegate can be easily set from Storyboard or via code with UITabBarController's delegate property.
myTabBar.delegate = myNewDelegate // Have to conform to UITabBarControllerDelegate protocol
You can even subclass UITabBarController and implement UITabBarControllerDelegate for it so it can become delegate for itself.
mySubclassedTabBar.delegate = mySubclassedTabBar
When you have delegate you can try out this method.
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool
{
guard let tabBarControllers = tabBarController.viewControllers
else
{
// TabBar have no configured controllers
return true
}
if let newIndex = tabBarControllers.indexOf(viewController) where newIndex == tabBarController.selectedIndex
{
// Index won't change so we can scroll
guard let tableViewController = viewController as? UITableViewController // Or any other condition
else
{
// We are not in UITableViewController
return true
}
tableViewController.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true)
}
return true
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if tabBarController.viewControllers!.firstIndex(of: viewController) == 0 {
if self.selectedIndex != 0 { return true }
if let navigationController = viewController as? UINavigationController{
if let streamController = navigationController.viewControllers.last as? HomeVC
{
streamController.tableView.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
}
}
return true
}
return true
}