Scroll a UIViewController to the top just like it is done when clicking on the iOS Status Bar












0















I would like to know if the method that Apple uses to scroll a UIViewController that contains a UIScrollView to the top when the user taps on the status bar is available to us.



I tried to find it, but I had no success.



I tried to reproduce it, but it did not work 100% when the UIViewController prefers to display large title (the large title is not expanded).



How can we scroll a UIViewController to the top just like Apple does?





Follows my trial:



extension UIViewController {

private var firstScrollView: UIScrollView? {
var scrollView: UIScrollView? = nil

var viewsToCheck = [self.view]
while !viewsToCheck.isEmpty && scrollView == nil {
let viewToCheck = viewsToCheck.remove(at: 0)!

if let viewToCheckAsScrollView = viewToCheck as? UIScrollView {
scrollView = viewToCheckAsScrollView
} else {
viewsToCheck.append(contentsOf: viewToCheck.subviews)
}
}

return scrollView
}

func scrollToTheTop(animated: Bool) {
if let firstScrollView = firstScrollView {
firstScrollView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
}
}
}









share|improve this question

























  • The behaviour you describe is a native behaviour if you are using a UIScrollView subclass (UITableView or a UIScrollView). It's working for me without adding any additional code.

    – Mourad
    Jan 2 at 16:10











  • @Mourad is there any way that I can call it? I want to do it programatically.

    – Augusto Carmo
    Jan 2 at 16:14











  • can you please explain? when clicking on the iOS Status Bar nothing happend to me . You mean swipe down from top of status bar to see notification?

    – user1376400
    Jan 2 at 16:22













  • The method you are using 'scrollToTheTop' is correct. But i think that your problem is due to the implementation you are doing to get the UIScrollView instance. I think that your while loop is not working, because the 'viewsToCheck' variable is not instantiated properly. I think that it should be initialized with self.view.subviews and not just self.view (I suppose that your UIScrollView instance is added as a subview to your UIViewController's view) I hope this helps.

    – Mourad
    Jan 2 at 16:32











  • You do not explain what “but it did not work 100%“ means, so those words are meaningless. When you use your code what happens?

    – matt
    Jan 2 at 16:41
















0















I would like to know if the method that Apple uses to scroll a UIViewController that contains a UIScrollView to the top when the user taps on the status bar is available to us.



I tried to find it, but I had no success.



I tried to reproduce it, but it did not work 100% when the UIViewController prefers to display large title (the large title is not expanded).



How can we scroll a UIViewController to the top just like Apple does?





Follows my trial:



extension UIViewController {

private var firstScrollView: UIScrollView? {
var scrollView: UIScrollView? = nil

var viewsToCheck = [self.view]
while !viewsToCheck.isEmpty && scrollView == nil {
let viewToCheck = viewsToCheck.remove(at: 0)!

if let viewToCheckAsScrollView = viewToCheck as? UIScrollView {
scrollView = viewToCheckAsScrollView
} else {
viewsToCheck.append(contentsOf: viewToCheck.subviews)
}
}

return scrollView
}

func scrollToTheTop(animated: Bool) {
if let firstScrollView = firstScrollView {
firstScrollView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
}
}
}









share|improve this question

























  • The behaviour you describe is a native behaviour if you are using a UIScrollView subclass (UITableView or a UIScrollView). It's working for me without adding any additional code.

    – Mourad
    Jan 2 at 16:10











  • @Mourad is there any way that I can call it? I want to do it programatically.

    – Augusto Carmo
    Jan 2 at 16:14











  • can you please explain? when clicking on the iOS Status Bar nothing happend to me . You mean swipe down from top of status bar to see notification?

    – user1376400
    Jan 2 at 16:22













  • The method you are using 'scrollToTheTop' is correct. But i think that your problem is due to the implementation you are doing to get the UIScrollView instance. I think that your while loop is not working, because the 'viewsToCheck' variable is not instantiated properly. I think that it should be initialized with self.view.subviews and not just self.view (I suppose that your UIScrollView instance is added as a subview to your UIViewController's view) I hope this helps.

    – Mourad
    Jan 2 at 16:32











  • You do not explain what “but it did not work 100%“ means, so those words are meaningless. When you use your code what happens?

    – matt
    Jan 2 at 16:41














0












0








0








I would like to know if the method that Apple uses to scroll a UIViewController that contains a UIScrollView to the top when the user taps on the status bar is available to us.



I tried to find it, but I had no success.



I tried to reproduce it, but it did not work 100% when the UIViewController prefers to display large title (the large title is not expanded).



How can we scroll a UIViewController to the top just like Apple does?





Follows my trial:



extension UIViewController {

private var firstScrollView: UIScrollView? {
var scrollView: UIScrollView? = nil

var viewsToCheck = [self.view]
while !viewsToCheck.isEmpty && scrollView == nil {
let viewToCheck = viewsToCheck.remove(at: 0)!

if let viewToCheckAsScrollView = viewToCheck as? UIScrollView {
scrollView = viewToCheckAsScrollView
} else {
viewsToCheck.append(contentsOf: viewToCheck.subviews)
}
}

return scrollView
}

func scrollToTheTop(animated: Bool) {
if let firstScrollView = firstScrollView {
firstScrollView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
}
}
}









share|improve this question
















I would like to know if the method that Apple uses to scroll a UIViewController that contains a UIScrollView to the top when the user taps on the status bar is available to us.



I tried to find it, but I had no success.



I tried to reproduce it, but it did not work 100% when the UIViewController prefers to display large title (the large title is not expanded).



How can we scroll a UIViewController to the top just like Apple does?





Follows my trial:



extension UIViewController {

private var firstScrollView: UIScrollView? {
var scrollView: UIScrollView? = nil

var viewsToCheck = [self.view]
while !viewsToCheck.isEmpty && scrollView == nil {
let viewToCheck = viewsToCheck.remove(at: 0)!

if let viewToCheckAsScrollView = viewToCheck as? UIScrollView {
scrollView = viewToCheckAsScrollView
} else {
viewsToCheck.append(contentsOf: viewToCheck.subviews)
}
}

return scrollView
}

func scrollToTheTop(animated: Bool) {
if let firstScrollView = firstScrollView {
firstScrollView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
}
}
}






ios tableview scrollview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 8:02







Augusto Carmo

















asked Jan 2 at 15:59









Augusto CarmoAugusto Carmo

863420




863420













  • The behaviour you describe is a native behaviour if you are using a UIScrollView subclass (UITableView or a UIScrollView). It's working for me without adding any additional code.

    – Mourad
    Jan 2 at 16:10











  • @Mourad is there any way that I can call it? I want to do it programatically.

    – Augusto Carmo
    Jan 2 at 16:14











  • can you please explain? when clicking on the iOS Status Bar nothing happend to me . You mean swipe down from top of status bar to see notification?

    – user1376400
    Jan 2 at 16:22













  • The method you are using 'scrollToTheTop' is correct. But i think that your problem is due to the implementation you are doing to get the UIScrollView instance. I think that your while loop is not working, because the 'viewsToCheck' variable is not instantiated properly. I think that it should be initialized with self.view.subviews and not just self.view (I suppose that your UIScrollView instance is added as a subview to your UIViewController's view) I hope this helps.

    – Mourad
    Jan 2 at 16:32











  • You do not explain what “but it did not work 100%“ means, so those words are meaningless. When you use your code what happens?

    – matt
    Jan 2 at 16:41



















  • The behaviour you describe is a native behaviour if you are using a UIScrollView subclass (UITableView or a UIScrollView). It's working for me without adding any additional code.

    – Mourad
    Jan 2 at 16:10











  • @Mourad is there any way that I can call it? I want to do it programatically.

    – Augusto Carmo
    Jan 2 at 16:14











  • can you please explain? when clicking on the iOS Status Bar nothing happend to me . You mean swipe down from top of status bar to see notification?

    – user1376400
    Jan 2 at 16:22













  • The method you are using 'scrollToTheTop' is correct. But i think that your problem is due to the implementation you are doing to get the UIScrollView instance. I think that your while loop is not working, because the 'viewsToCheck' variable is not instantiated properly. I think that it should be initialized with self.view.subviews and not just self.view (I suppose that your UIScrollView instance is added as a subview to your UIViewController's view) I hope this helps.

    – Mourad
    Jan 2 at 16:32











  • You do not explain what “but it did not work 100%“ means, so those words are meaningless. When you use your code what happens?

    – matt
    Jan 2 at 16:41

















The behaviour you describe is a native behaviour if you are using a UIScrollView subclass (UITableView or a UIScrollView). It's working for me without adding any additional code.

– Mourad
Jan 2 at 16:10





The behaviour you describe is a native behaviour if you are using a UIScrollView subclass (UITableView or a UIScrollView). It's working for me without adding any additional code.

– Mourad
Jan 2 at 16:10













@Mourad is there any way that I can call it? I want to do it programatically.

– Augusto Carmo
Jan 2 at 16:14





@Mourad is there any way that I can call it? I want to do it programatically.

– Augusto Carmo
Jan 2 at 16:14













can you please explain? when clicking on the iOS Status Bar nothing happend to me . You mean swipe down from top of status bar to see notification?

– user1376400
Jan 2 at 16:22







can you please explain? when clicking on the iOS Status Bar nothing happend to me . You mean swipe down from top of status bar to see notification?

– user1376400
Jan 2 at 16:22















The method you are using 'scrollToTheTop' is correct. But i think that your problem is due to the implementation you are doing to get the UIScrollView instance. I think that your while loop is not working, because the 'viewsToCheck' variable is not instantiated properly. I think that it should be initialized with self.view.subviews and not just self.view (I suppose that your UIScrollView instance is added as a subview to your UIViewController's view) I hope this helps.

– Mourad
Jan 2 at 16:32





The method you are using 'scrollToTheTop' is correct. But i think that your problem is due to the implementation you are doing to get the UIScrollView instance. I think that your while loop is not working, because the 'viewsToCheck' variable is not instantiated properly. I think that it should be initialized with self.view.subviews and not just self.view (I suppose that your UIScrollView instance is added as a subview to your UIViewController's view) I hope this helps.

– Mourad
Jan 2 at 16:32













You do not explain what “but it did not work 100%“ means, so those words are meaningless. When you use your code what happens?

– matt
Jan 2 at 16:41





You do not explain what “but it did not work 100%“ means, so those words are meaningless. When you use your code what happens?

– matt
Jan 2 at 16:41












1 Answer
1






active

oldest

votes


















0














You have to take account of the scroll view’s content inset (adjustedContentInset) when working out where to scroll to. Your code does not do that, so you end up scrolling to the wrong place.



firstScrollView.contentOffset.y = -firstScrollView.adjustedContentInset.top





share|improve this answer


























  • Thank you for your contribution, @matt. But this code is having the same behaviour as the one that I posted. In case the UIViewController prefers to display large title, when the scrolling is done, the large title is not expanded =/.

    – Augusto Carmo
    Jan 3 at 8:10











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009404%2fscroll-a-uiviewcontroller-to-the-top-just-like-it-is-done-when-clicking-on-the-i%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You have to take account of the scroll view’s content inset (adjustedContentInset) when working out where to scroll to. Your code does not do that, so you end up scrolling to the wrong place.



firstScrollView.contentOffset.y = -firstScrollView.adjustedContentInset.top





share|improve this answer


























  • Thank you for your contribution, @matt. But this code is having the same behaviour as the one that I posted. In case the UIViewController prefers to display large title, when the scrolling is done, the large title is not expanded =/.

    – Augusto Carmo
    Jan 3 at 8:10
















0














You have to take account of the scroll view’s content inset (adjustedContentInset) when working out where to scroll to. Your code does not do that, so you end up scrolling to the wrong place.



firstScrollView.contentOffset.y = -firstScrollView.adjustedContentInset.top





share|improve this answer


























  • Thank you for your contribution, @matt. But this code is having the same behaviour as the one that I posted. In case the UIViewController prefers to display large title, when the scrolling is done, the large title is not expanded =/.

    – Augusto Carmo
    Jan 3 at 8:10














0












0








0







You have to take account of the scroll view’s content inset (adjustedContentInset) when working out where to scroll to. Your code does not do that, so you end up scrolling to the wrong place.



firstScrollView.contentOffset.y = -firstScrollView.adjustedContentInset.top





share|improve this answer















You have to take account of the scroll view’s content inset (adjustedContentInset) when working out where to scroll to. Your code does not do that, so you end up scrolling to the wrong place.



firstScrollView.contentOffset.y = -firstScrollView.adjustedContentInset.top






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 16:35

























answered Jan 2 at 16:29









mattmatt

333k46544742




333k46544742













  • Thank you for your contribution, @matt. But this code is having the same behaviour as the one that I posted. In case the UIViewController prefers to display large title, when the scrolling is done, the large title is not expanded =/.

    – Augusto Carmo
    Jan 3 at 8:10



















  • Thank you for your contribution, @matt. But this code is having the same behaviour as the one that I posted. In case the UIViewController prefers to display large title, when the scrolling is done, the large title is not expanded =/.

    – Augusto Carmo
    Jan 3 at 8:10

















Thank you for your contribution, @matt. But this code is having the same behaviour as the one that I posted. In case the UIViewController prefers to display large title, when the scrolling is done, the large title is not expanded =/.

– Augusto Carmo
Jan 3 at 8:10





Thank you for your contribution, @matt. But this code is having the same behaviour as the one that I posted. In case the UIViewController prefers to display large title, when the scrolling is done, the large title is not expanded =/.

– Augusto Carmo
Jan 3 at 8:10




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009404%2fscroll-a-uiviewcontroller-to-the-top-just-like-it-is-done-when-clicking-on-the-i%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory