简体   繁体   中英

Having trouble finding y positions of elements inside scrollview using frame.origin.y

I have 2 elements inside a scrollview.

Element 1 gives out 390.0 after finding its y position using frame.origin.y

As for Element 2, I've attempted to place it in the exact same position as Element 1 (y = 390.0) by writing below :

Element2.frame.origin.y = yPosition

Unfortunately, Element 2 moves upwards only slightly (its was originally far below Element 1) ... it doesn't move to the position given by Element 1 yet the frame.origin.y of both elements are now the same. What could be the problem?

The reason is that the frame of the element 1 is not converted to self.view , so it;s relative to the scrollView , not to self.view , You can try

 let ret = self.elem1.convert(self.elem1.frame, to: self.view)

 print(ret.origin.y)

First, convert the scroll view's coordinate to the main view's coordinate perspective. Then, access the the y value through the origin

let ret = self.elem1.convert(self.elem1.frame, to: self.view).origin.y

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