Programmatically scroll UIScrollView so UILabel is just below navigation bar











up vote
1
down vote

favorite












I have a UIScrollView that contains various blocks of buttons, textfields and labels.



There is a UILabel heading the second block. When I tap a button beside it, I wish to programmatically scroll so this label is at the top left of the screen, just below the navigation bar.



I can use the setContentOffset function to do this programmatically, but I can't work out how to programmatically get the value for y. As the top block can expand or contract based on user input I need to get this value programmatically. How do I get the correct value of y for my label?



let myLabelTopYPoint = ??!


I played around with the following two lines but neither gave what I wanted...



let myLabelTopYPoint = myLabel.convert(myLabel.center, to: self.view).y

let myLabelTopYPoint = myLabel.frame.maxY


(Applying the below with a manually chosen y of 200 works in the meantime for most user inputs, but is not satisfactory)



myScrollView.setContentOffset(CGPoint(x: 0, y: myLabelTopYPoint), animated: true)









share|improve this question


























    up vote
    1
    down vote

    favorite












    I have a UIScrollView that contains various blocks of buttons, textfields and labels.



    There is a UILabel heading the second block. When I tap a button beside it, I wish to programmatically scroll so this label is at the top left of the screen, just below the navigation bar.



    I can use the setContentOffset function to do this programmatically, but I can't work out how to programmatically get the value for y. As the top block can expand or contract based on user input I need to get this value programmatically. How do I get the correct value of y for my label?



    let myLabelTopYPoint = ??!


    I played around with the following two lines but neither gave what I wanted...



    let myLabelTopYPoint = myLabel.convert(myLabel.center, to: self.view).y

    let myLabelTopYPoint = myLabel.frame.maxY


    (Applying the below with a manually chosen y of 200 works in the meantime for most user inputs, but is not satisfactory)



    myScrollView.setContentOffset(CGPoint(x: 0, y: myLabelTopYPoint), animated: true)









    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a UIScrollView that contains various blocks of buttons, textfields and labels.



      There is a UILabel heading the second block. When I tap a button beside it, I wish to programmatically scroll so this label is at the top left of the screen, just below the navigation bar.



      I can use the setContentOffset function to do this programmatically, but I can't work out how to programmatically get the value for y. As the top block can expand or contract based on user input I need to get this value programmatically. How do I get the correct value of y for my label?



      let myLabelTopYPoint = ??!


      I played around with the following two lines but neither gave what I wanted...



      let myLabelTopYPoint = myLabel.convert(myLabel.center, to: self.view).y

      let myLabelTopYPoint = myLabel.frame.maxY


      (Applying the below with a manually chosen y of 200 works in the meantime for most user inputs, but is not satisfactory)



      myScrollView.setContentOffset(CGPoint(x: 0, y: myLabelTopYPoint), animated: true)









      share|improve this question













      I have a UIScrollView that contains various blocks of buttons, textfields and labels.



      There is a UILabel heading the second block. When I tap a button beside it, I wish to programmatically scroll so this label is at the top left of the screen, just below the navigation bar.



      I can use the setContentOffset function to do this programmatically, but I can't work out how to programmatically get the value for y. As the top block can expand or contract based on user input I need to get this value programmatically. How do I get the correct value of y for my label?



      let myLabelTopYPoint = ??!


      I played around with the following two lines but neither gave what I wanted...



      let myLabelTopYPoint = myLabel.convert(myLabel.center, to: self.view).y

      let myLabelTopYPoint = myLabel.frame.maxY


      (Applying the below with a manually chosen y of 200 works in the meantime for most user inputs, but is not satisfactory)



      myScrollView.setContentOffset(CGPoint(x: 0, y: myLabelTopYPoint), animated: true)






      ios swift






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 7:53









      Daniel

      305




      305
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can get y position of your label in scroll view like this:



          let myLabelTopYPoint = yourScrollView.convert(myLabel.frame, to: yourScrollView).minY





          share|improve this answer























          • Thanks, but that always gives me "0" - I need the point relative to the navigation bar rather than to itself. Unless I'm doing something wrong?
            – Daniel
            Nov 11 at 8:17










          • Try edited answer
            – Robert Dresler
            Nov 11 at 8:32










          • Unfortunately again myLabel.frame gives 0,0 so scrolls to the top of the page. Thanks for trying
            – Daniel
            Nov 11 at 8:49










          • Didn't you forget to use yourScrollView.convert instead of view.convert?
            – Robert Dresler
            Nov 11 at 8:50










          • Seems as a correct answer to me. Or may be your scrollView's top is constrained to the top of the screen instead of navigation bar.
            – kelin
            Nov 11 at 8:56











          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',
          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%2f53246819%2fprogrammatically-scroll-uiscrollview-so-uilabel-is-just-below-navigation-bar%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








          up vote
          1
          down vote



          accepted










          You can get y position of your label in scroll view like this:



          let myLabelTopYPoint = yourScrollView.convert(myLabel.frame, to: yourScrollView).minY





          share|improve this answer























          • Thanks, but that always gives me "0" - I need the point relative to the navigation bar rather than to itself. Unless I'm doing something wrong?
            – Daniel
            Nov 11 at 8:17










          • Try edited answer
            – Robert Dresler
            Nov 11 at 8:32










          • Unfortunately again myLabel.frame gives 0,0 so scrolls to the top of the page. Thanks for trying
            – Daniel
            Nov 11 at 8:49










          • Didn't you forget to use yourScrollView.convert instead of view.convert?
            – Robert Dresler
            Nov 11 at 8:50










          • Seems as a correct answer to me. Or may be your scrollView's top is constrained to the top of the screen instead of navigation bar.
            – kelin
            Nov 11 at 8:56















          up vote
          1
          down vote



          accepted










          You can get y position of your label in scroll view like this:



          let myLabelTopYPoint = yourScrollView.convert(myLabel.frame, to: yourScrollView).minY





          share|improve this answer























          • Thanks, but that always gives me "0" - I need the point relative to the navigation bar rather than to itself. Unless I'm doing something wrong?
            – Daniel
            Nov 11 at 8:17










          • Try edited answer
            – Robert Dresler
            Nov 11 at 8:32










          • Unfortunately again myLabel.frame gives 0,0 so scrolls to the top of the page. Thanks for trying
            – Daniel
            Nov 11 at 8:49










          • Didn't you forget to use yourScrollView.convert instead of view.convert?
            – Robert Dresler
            Nov 11 at 8:50










          • Seems as a correct answer to me. Or may be your scrollView's top is constrained to the top of the screen instead of navigation bar.
            – kelin
            Nov 11 at 8:56













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You can get y position of your label in scroll view like this:



          let myLabelTopYPoint = yourScrollView.convert(myLabel.frame, to: yourScrollView).minY





          share|improve this answer














          You can get y position of your label in scroll view like this:



          let myLabelTopYPoint = yourScrollView.convert(myLabel.frame, to: yourScrollView).minY






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 8:32

























          answered Nov 11 at 8:08









          Robert Dresler

          1,456218




          1,456218












          • Thanks, but that always gives me "0" - I need the point relative to the navigation bar rather than to itself. Unless I'm doing something wrong?
            – Daniel
            Nov 11 at 8:17










          • Try edited answer
            – Robert Dresler
            Nov 11 at 8:32










          • Unfortunately again myLabel.frame gives 0,0 so scrolls to the top of the page. Thanks for trying
            – Daniel
            Nov 11 at 8:49










          • Didn't you forget to use yourScrollView.convert instead of view.convert?
            – Robert Dresler
            Nov 11 at 8:50










          • Seems as a correct answer to me. Or may be your scrollView's top is constrained to the top of the screen instead of navigation bar.
            – kelin
            Nov 11 at 8:56


















          • Thanks, but that always gives me "0" - I need the point relative to the navigation bar rather than to itself. Unless I'm doing something wrong?
            – Daniel
            Nov 11 at 8:17










          • Try edited answer
            – Robert Dresler
            Nov 11 at 8:32










          • Unfortunately again myLabel.frame gives 0,0 so scrolls to the top of the page. Thanks for trying
            – Daniel
            Nov 11 at 8:49










          • Didn't you forget to use yourScrollView.convert instead of view.convert?
            – Robert Dresler
            Nov 11 at 8:50










          • Seems as a correct answer to me. Or may be your scrollView's top is constrained to the top of the screen instead of navigation bar.
            – kelin
            Nov 11 at 8:56
















          Thanks, but that always gives me "0" - I need the point relative to the navigation bar rather than to itself. Unless I'm doing something wrong?
          – Daniel
          Nov 11 at 8:17




          Thanks, but that always gives me "0" - I need the point relative to the navigation bar rather than to itself. Unless I'm doing something wrong?
          – Daniel
          Nov 11 at 8:17












          Try edited answer
          – Robert Dresler
          Nov 11 at 8:32




          Try edited answer
          – Robert Dresler
          Nov 11 at 8:32












          Unfortunately again myLabel.frame gives 0,0 so scrolls to the top of the page. Thanks for trying
          – Daniel
          Nov 11 at 8:49




          Unfortunately again myLabel.frame gives 0,0 so scrolls to the top of the page. Thanks for trying
          – Daniel
          Nov 11 at 8:49












          Didn't you forget to use yourScrollView.convert instead of view.convert?
          – Robert Dresler
          Nov 11 at 8:50




          Didn't you forget to use yourScrollView.convert instead of view.convert?
          – Robert Dresler
          Nov 11 at 8:50












          Seems as a correct answer to me. Or may be your scrollView's top is constrained to the top of the screen instead of navigation bar.
          – kelin
          Nov 11 at 8:56




          Seems as a correct answer to me. Or may be your scrollView's top is constrained to the top of the screen instead of navigation bar.
          – kelin
          Nov 11 at 8:56


















          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.





          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246819%2fprogrammatically-scroll-uiscrollview-so-uilabel-is-just-below-navigation-bar%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

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma