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)
ios swift
add a comment |
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)
ios swift
add a comment |
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)
ios swift
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
ios swift
asked Nov 11 at 7:53
Daniel
305
305
add a comment |
add a comment |
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
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
|
show 1 more 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
You can get y position of your label in scroll view like this:
let myLabelTopYPoint = yourScrollView.convert(myLabel.frame, to: yourScrollView).minY
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
|
show 1 more comment
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
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
|
show 1 more comment
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
You can get y position of your label in scroll view like this:
let myLabelTopYPoint = yourScrollView.convert(myLabel.frame, to: yourScrollView).minY
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
|
show 1 more comment
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
|
show 1 more 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%2f53246819%2fprogrammatically-scroll-uiscrollview-so-uilabel-is-just-below-navigation-bar%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