Tab Bar scroll to the top of UIViewController when pressed twice
up vote
2
down vote
favorite
I'm trying to make my UITabBarController
scroll to the top of the page when pressed twice. I have tried several times to get this to work with no luck. As of now, I only have one class for the UITabBarController
on the storyboard that is linked to the code. Am I supposed to link UITabBar
as well? Here's my code that I've attempted so far.
import UIKit
class TabViewController: UITabBarController, UITabBarControllerDelegate {
var pressedCount: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
// Do any additional setup after loading the view.
}
@IBAction func unwindToMain(segue: UIStoryboardSegue) {}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("Selected item")
}
// UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print("Selected view controller")
}
func tabBarController(_ TabViewController: 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 as UIScrollView:
if scrollView.scrollsToTop == true {
scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true)
return
}
default:
break
}
for subView in view.subviews {
scrollToTop(view: subView)
}
}
scrollToTop(view: view)
}
var isScrolledToTop: Bool {
if self is UITableViewController {
return (self as! UITableViewController).tableView.contentOffset.y == 0
}
for subView in view.subviews {
if let scrollView = subView as? UIScrollView {
return (scrollView.contentOffset.y == 0)
}
}
return true
}
}
swift xcode uiviewcontroller uitabbarcontroller uitabbar
add a comment |
up vote
2
down vote
favorite
I'm trying to make my UITabBarController
scroll to the top of the page when pressed twice. I have tried several times to get this to work with no luck. As of now, I only have one class for the UITabBarController
on the storyboard that is linked to the code. Am I supposed to link UITabBar
as well? Here's my code that I've attempted so far.
import UIKit
class TabViewController: UITabBarController, UITabBarControllerDelegate {
var pressedCount: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
// Do any additional setup after loading the view.
}
@IBAction func unwindToMain(segue: UIStoryboardSegue) {}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("Selected item")
}
// UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print("Selected view controller")
}
func tabBarController(_ TabViewController: 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 as UIScrollView:
if scrollView.scrollsToTop == true {
scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true)
return
}
default:
break
}
for subView in view.subviews {
scrollToTop(view: subView)
}
}
scrollToTop(view: view)
}
var isScrolledToTop: Bool {
if self is UITableViewController {
return (self as! UITableViewController).tableView.contentOffset.y == 0
}
for subView in view.subviews {
if let scrollView = subView as? UIScrollView {
return (scrollView.contentOffset.y == 0)
}
}
return true
}
}
swift xcode uiviewcontroller uitabbarcontroller uitabbar
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to make my UITabBarController
scroll to the top of the page when pressed twice. I have tried several times to get this to work with no luck. As of now, I only have one class for the UITabBarController
on the storyboard that is linked to the code. Am I supposed to link UITabBar
as well? Here's my code that I've attempted so far.
import UIKit
class TabViewController: UITabBarController, UITabBarControllerDelegate {
var pressedCount: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
// Do any additional setup after loading the view.
}
@IBAction func unwindToMain(segue: UIStoryboardSegue) {}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("Selected item")
}
// UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print("Selected view controller")
}
func tabBarController(_ TabViewController: 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 as UIScrollView:
if scrollView.scrollsToTop == true {
scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true)
return
}
default:
break
}
for subView in view.subviews {
scrollToTop(view: subView)
}
}
scrollToTop(view: view)
}
var isScrolledToTop: Bool {
if self is UITableViewController {
return (self as! UITableViewController).tableView.contentOffset.y == 0
}
for subView in view.subviews {
if let scrollView = subView as? UIScrollView {
return (scrollView.contentOffset.y == 0)
}
}
return true
}
}
swift xcode uiviewcontroller uitabbarcontroller uitabbar
I'm trying to make my UITabBarController
scroll to the top of the page when pressed twice. I have tried several times to get this to work with no luck. As of now, I only have one class for the UITabBarController
on the storyboard that is linked to the code. Am I supposed to link UITabBar
as well? Here's my code that I've attempted so far.
import UIKit
class TabViewController: UITabBarController, UITabBarControllerDelegate {
var pressedCount: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
// Do any additional setup after loading the view.
}
@IBAction func unwindToMain(segue: UIStoryboardSegue) {}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("Selected item")
}
// UITabBarControllerDelegate
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
print("Selected view controller")
}
func tabBarController(_ TabViewController: 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 as UIScrollView:
if scrollView.scrollsToTop == true {
scrollView.setContentOffset(CGPoint(x: 0.0, y: -scrollView.contentInset.top), animated: true)
return
}
default:
break
}
for subView in view.subviews {
scrollToTop(view: subView)
}
}
scrollToTop(view: view)
}
var isScrolledToTop: Bool {
if self is UITableViewController {
return (self as! UITableViewController).tableView.contentOffset.y == 0
}
for subView in view.subviews {
if let scrollView = subView as? UIScrollView {
return (scrollView.contentOffset.y == 0)
}
}
return true
}
}
swift xcode uiviewcontroller uitabbarcontroller uitabbar
swift xcode uiviewcontroller uitabbarcontroller uitabbar
edited Nov 13 at 4:47
asked Nov 13 at 3:13
Nigel Denny
697
697
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
For example:
var pressedCount: Int = 0
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
pressedCount += 1
if pressedCount > 1 {
scrollToTop(view: self.view)
} else {
//do something for first press
}
}
Do I make a new controller for UITabBar or do I implement this into the rest of my code
– Nigel Denny
Nov 13 at 4:40
try typing in func tabBarController(_ and look for didSelectItem
– Alex Bailey
Nov 13 at 4:42
if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.
– Alex Bailey
Nov 13 at 4:43
I've edited my code and I can now track when the tab bar is pressed, where would I implement your code
– Nigel Denny
Nov 13 at 4:48
inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 { scrollToTop(view: self.view) } else { //do something for first press } }
– Alex Bailey
Nov 13 at 4:49
|
show 3 more comments
up vote
0
down vote
Customize the UITabBar and then use the notification time to make the outside world respond!Or record the number of clicks through the UITabBarController agent and then notify the outside world to respond!
1
A little new to coding, could you give me an example of this?
– Nigel Denny
Nov 13 at 3:59
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
For example:
var pressedCount: Int = 0
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
pressedCount += 1
if pressedCount > 1 {
scrollToTop(view: self.view)
} else {
//do something for first press
}
}
Do I make a new controller for UITabBar or do I implement this into the rest of my code
– Nigel Denny
Nov 13 at 4:40
try typing in func tabBarController(_ and look for didSelectItem
– Alex Bailey
Nov 13 at 4:42
if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.
– Alex Bailey
Nov 13 at 4:43
I've edited my code and I can now track when the tab bar is pressed, where would I implement your code
– Nigel Denny
Nov 13 at 4:48
inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 { scrollToTop(view: self.view) } else { //do something for first press } }
– Alex Bailey
Nov 13 at 4:49
|
show 3 more comments
up vote
1
down vote
accepted
For example:
var pressedCount: Int = 0
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
pressedCount += 1
if pressedCount > 1 {
scrollToTop(view: self.view)
} else {
//do something for first press
}
}
Do I make a new controller for UITabBar or do I implement this into the rest of my code
– Nigel Denny
Nov 13 at 4:40
try typing in func tabBarController(_ and look for didSelectItem
– Alex Bailey
Nov 13 at 4:42
if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.
– Alex Bailey
Nov 13 at 4:43
I've edited my code and I can now track when the tab bar is pressed, where would I implement your code
– Nigel Denny
Nov 13 at 4:48
inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 { scrollToTop(view: self.view) } else { //do something for first press } }
– Alex Bailey
Nov 13 at 4:49
|
show 3 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
For example:
var pressedCount: Int = 0
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
pressedCount += 1
if pressedCount > 1 {
scrollToTop(view: self.view)
} else {
//do something for first press
}
}
For example:
var pressedCount: Int = 0
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
pressedCount += 1
if pressedCount > 1 {
scrollToTop(view: self.view)
} else {
//do something for first press
}
}
answered Nov 13 at 4:36
Alex Bailey
312214
312214
Do I make a new controller for UITabBar or do I implement this into the rest of my code
– Nigel Denny
Nov 13 at 4:40
try typing in func tabBarController(_ and look for didSelectItem
– Alex Bailey
Nov 13 at 4:42
if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.
– Alex Bailey
Nov 13 at 4:43
I've edited my code and I can now track when the tab bar is pressed, where would I implement your code
– Nigel Denny
Nov 13 at 4:48
inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 { scrollToTop(view: self.view) } else { //do something for first press } }
– Alex Bailey
Nov 13 at 4:49
|
show 3 more comments
Do I make a new controller for UITabBar or do I implement this into the rest of my code
– Nigel Denny
Nov 13 at 4:40
try typing in func tabBarController(_ and look for didSelectItem
– Alex Bailey
Nov 13 at 4:42
if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.
– Alex Bailey
Nov 13 at 4:43
I've edited my code and I can now track when the tab bar is pressed, where would I implement your code
– Nigel Denny
Nov 13 at 4:48
inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 { scrollToTop(view: self.view) } else { //do something for first press } }
– Alex Bailey
Nov 13 at 4:49
Do I make a new controller for UITabBar or do I implement this into the rest of my code
– Nigel Denny
Nov 13 at 4:40
Do I make a new controller for UITabBar or do I implement this into the rest of my code
– Nigel Denny
Nov 13 at 4:40
try typing in func tabBarController(_ and look for didSelectItem
– Alex Bailey
Nov 13 at 4:42
try typing in func tabBarController(_ and look for didSelectItem
– Alex Bailey
Nov 13 at 4:42
if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.
– Alex Bailey
Nov 13 at 4:43
if you know how to call an action if the tab bar is pressed once just use this code to increment on that action.
– Alex Bailey
Nov 13 at 4:43
I've edited my code and I can now track when the tab bar is pressed, where would I implement your code
– Nigel Denny
Nov 13 at 4:48
I've edited my code and I can now track when the tab bar is pressed, where would I implement your code
– Nigel Denny
Nov 13 at 4:48
inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 { scrollToTop(view: self.view) } else { //do something for first press } }
– Alex Bailey
Nov 13 at 4:49
inside the function where the tab bar is pressed add pressedCount += 1 if pressedCount > 1 { scrollToTop(view: self.view) } else { //do something for first press } }
– Alex Bailey
Nov 13 at 4:49
|
show 3 more comments
up vote
0
down vote
Customize the UITabBar and then use the notification time to make the outside world respond!Or record the number of clicks through the UITabBarController agent and then notify the outside world to respond!
1
A little new to coding, could you give me an example of this?
– Nigel Denny
Nov 13 at 3:59
add a comment |
up vote
0
down vote
Customize the UITabBar and then use the notification time to make the outside world respond!Or record the number of clicks through the UITabBarController agent and then notify the outside world to respond!
1
A little new to coding, could you give me an example of this?
– Nigel Denny
Nov 13 at 3:59
add a comment |
up vote
0
down vote
up vote
0
down vote
Customize the UITabBar and then use the notification time to make the outside world respond!Or record the number of clicks through the UITabBarController agent and then notify the outside world to respond!
Customize the UITabBar and then use the notification time to make the outside world respond!Or record the number of clicks through the UITabBarController agent and then notify the outside world to respond!
answered Nov 13 at 3:46
itzhangbao
1
1
1
A little new to coding, could you give me an example of this?
– Nigel Denny
Nov 13 at 3:59
add a comment |
1
A little new to coding, could you give me an example of this?
– Nigel Denny
Nov 13 at 3:59
1
1
A little new to coding, could you give me an example of this?
– Nigel Denny
Nov 13 at 3:59
A little new to coding, could you give me an example of this?
– Nigel Denny
Nov 13 at 3:59
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273249%2ftab-bar-scroll-to-the-top-of-uiviewcontroller-when-pressed-twice%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown