简体   繁体   中英

Programmatically scroll UIScrollView so UILabel is just below navigation bar

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)

您可以在滚动视图中获得标签的 y 位置,如下所示:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM