Scroll to the top by second tapped to Bar Item
up vote
0
down vote
favorite
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
ios swift uitabbarcontroller
add a comment |
up vote
0
down vote
favorite
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
ios swift uitabbarcontroller
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
ios swift uitabbarcontroller
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
ios swift uitabbarcontroller
ios swift uitabbarcontroller
edited Nov 22 at 10:09
asked Nov 22 at 9:38
Stanislav Putilov
298
298
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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)
}
}
}
Thanks, works nice!
– Stanislav Putilov
Nov 22 at 10:25
@StanislavPutilov please accept the answer if it worked.
– inokey
Nov 22 at 11:02
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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)
}
}
}
Thanks, works nice!
– Stanislav Putilov
Nov 22 at 10:25
@StanislavPutilov please accept the answer if it worked.
– inokey
Nov 22 at 11:02
add a comment |
up vote
1
down vote
accepted
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)
}
}
}
Thanks, works nice!
– Stanislav Putilov
Nov 22 at 10:25
@StanislavPutilov please accept the answer if it worked.
– inokey
Nov 22 at 11:02
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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)
}
}
}
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)
}
}
}
answered Nov 22 at 10:01
inokey
1,060617
1,060617
Thanks, works nice!
– Stanislav Putilov
Nov 22 at 10:25
@StanislavPutilov please accept the answer if it worked.
– inokey
Nov 22 at 11:02
add a comment |
Thanks, works nice!
– Stanislav Putilov
Nov 22 at 10:25
@StanislavPutilov please accept the answer if it worked.
– inokey
Nov 22 at 11:02
Thanks, works nice!
– Stanislav Putilov
Nov 22 at 10:25
Thanks, works nice!
– Stanislav Putilov
Nov 22 at 10:25
@StanislavPutilov please accept the answer if it worked.
– inokey
Nov 22 at 11:02
@StanislavPutilov please accept the answer if it worked.
– inokey
Nov 22 at 11:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53427857%2fscroll-to-the-top-by-second-tapped-to-bar-item%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