ios - Tap UITabBarItem To Scroll To Top Like Instagram and Twitter -
i'm having issues making feature work , help.
my hierarchy tabbarcontroller
-> navigation controller
-> tableviewcontroller
what want if on current tab , scrolled down able tap current view's uitabbaritem
, scrolled top,like instagram , twitter example.
i have tried many things right here :
but sadly non of answers did job me.
i appreciate manner , thank in advance!
here tableview`controller's code :
import uikit class barsviewcontroller: uitableviewcontroller,uisearchresultsupdating,uisearchbardelegate,uisearchdisplaydelegate,uitabbarcontrollerdelegate{ //tableview data & non related stuff.... override func scrollviewwillbegindragging(_ scrollview: uiscrollview) { self.searchcontroller.searchbar.resignfirstresponder() self.searchcontroller.searchbar.endediting(true) } func tabbarcontroller(_ tabbarcontroller: uitabbarcontroller, didselect viewcontroller: uiviewcontroller) { let tabbarindex = tabbarcontroller.selectedindex if tabbarindex == 0 { let indexpath = indexpath(row: 0, section: 0) let navigvc = viewcontroller as? uinavigationcontroller let finalvc = navigvc?.viewcontrollers[0] as? barsviewcontroller finalvc?.tableview.scrolltorow(at: indexpath indexpath, at: .top, animated: true) } } }
tabbarcontroller.swift code ( code doesn't work ) :
import uikit class tabbarcontroller: uitabbarcontroller,uitabbarcontrollerdelegate { override func viewdidload() { super.viewdidload() // additional setup after loading view. self.delegate = self } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func tabbarcontroller(_ tabbarcontroller: uitabbarcontroller, shouldselect viewcontroller: uiviewcontroller) -> bool { guard let viewcontrollers = viewcontrollers else { return false } if viewcontroller == viewcontrollers[selectedindex] { if let nav = viewcontroller as? uinavigationcontroller { guard let topcontroller = nav.viewcontrollers.last else { return true } if !topcontroller.isscrolledtotop { topcontroller.scrolltotop() return false } else { nav.popviewcontroller(animated: true) } return true } } return true } } extension uiviewcontroller { func scrolltotop() { func scrolltotop(view: uiview?) { guard let view = view else { return } switch view { case let scrollview uiscrollview: if scrollview.scrollstotop == true { scrollview.setcontentoffset(cgpoint(x: 0.0, y: -scrollview.contentinset.top), animated: true) return } default: break } subview in view.subviews { scrolltotop(view: subview) } } scrolltotop(view: view) } var isscrolledtotop: bool { subview in view.subviews { if let scrollview = subview as? uiscrollview { return (scrollview.contentoffset.y == 0) } } return true } }
here go, should work:
func tabbarcontroller(_ tabbarcontroller: uitabbarcontroller, shouldselect viewcontroller: uiviewcontroller) -> bool { guard let viewcontrollers = viewcontrollers else { return false } if viewcontroller == viewcontrollers[selectedindex] { if let nav = viewcontroller as? zbnavigationcontroller { guard let topcontroller = nav.viewcontrollers.last else { return true } if !topcontroller.isscrolledtotop { topcontroller.scrolltotop() return false } else { nav.popviewcontroller(animated: true) } return true } } return true }
and then...
extension uiviewcontroller { func scrolltotop() { func scrolltotop(view: uiview?) { guard let view = view else { return } switch view { case let scrollview uiscrollview: if scrollview.scrollstotop == true { scrollview.setcontentoffset(cgpoint(x: 0.0, y: -scrollview.contentinset.top), animated: true) return } default: break } subview in view.subviews { scrolltotop(view: subview) } } scrolltotop(view: view) } // changed var isscrolledtotop: bool { if self uitableviewcontroller { return (self as! uitableviewcontroller).tableview.contentoffset.y == 0 } subview in view.subviews { if let scrollview = subview as? uiscrollview { return (scrollview.contentoffset.y == 0) } } return true
}
there's bit in function if uiviewcontroller
@ top pop to previous controller
Comments
Post a Comment