How to get Y co-ordinate of lastview in ScrollView

Hi


How to get Y co-ordinate of lastview in ScrollView . Below code is not working.


func calculateContentSize(scrollView: UIScrollView) -> CGSize {
        var topPoint = CGFloat()
        var height = CGFloat()
       

        for subview in scrollView.subviews {
//            if subview.accessibilityIdentifier = clcViewB
//            {
//                print("Hello")
//            }
            if subview.frame.origin.y > topPoint {
                topPoint = subview.frame.origin.y
                height = subview.frame.size.height
            }
        }
        print(topPoint)
        return CGSize(width: scrollView.frame.size.width, height: height + topPoint)
    }


Thanks

Replies

What doesn't work ? What do you get with print(topPoint)


What do you get if you insert log:


        for subview in scrollView.subviews {
            Swift.print(subview, subview.frame.origin.y)
            if subview.frame.origin.y > topPoint { 
                topPoint = subview.frame.origin.y
                height = subview.frame.size.height
            }
        }
        print(topPoint)
        return CGSize(width: scrollView.frame.size.width, height: height + topPoint)
    }


At least I would test :


if subview.frame.origin.y >= topPoint


to make sure height is updated.

Also:


— What is "last view" supposed to mean? The loop finds the largest subview origin (the origin that's "last" on the screen), but a view with a higher origin may still extend further down because of a larger height.


— Initializing the variables in lines 02-03 to "CGFloat ()" sets them to zero, which is a logic error if subviews have negative frame.origin.y values.


— It's generally an error to iterate through (and make assumptions about) the subviews of a view whose structure you don't directly control. Scroll views may have subviews other than the one(s) you add to it.


— What's wrong with using the scroll view's "contentSize"?

Hi


To get Width & Height for Contentsize i am doing this . I don't want to hardcode. I want to calculate dynamically.

In a View Controller i have multiple UiView.


Thanks

but a view with a higher origin may still extend further down because of a larger height


that's right, I was thinking OS X not IOS.


So should Test bottomPoint and not topPoint and origin.y + height.

I don't understand. How is using "contentSize" hardcoding?


The scroll view uses "contentSize" to set the limits of the scroll bars (how far you can scroll). The only difference in calculating this yourself would be if you had some subviews that the user cannot scroll to.


Anyway, as far as looping through subviews is concerned:


for subview in scrollView.subviews {
     …
}


you can do it if you test for subviews belonging to you, and skipping over any others. You can test for your UIView subclasses, or view "tag" values that you set, or you can keep a separate list of your subviews (but then you'd be better off looping through that list).

Hi


Can i get only only Y co-ordinate of last UIView in ScrollView . In the below i want (468+128)


<UIView: 0x7fb84a45cdf0; frame = (0 1; 320 140); autoresize = LM+W+RM+TM+H+BM; layer = <CALayer: 0x7fb84a45cf60>>
<UIView: 0x7fb84a462b10; frame = (0 286; 320 128); autoresize = LM+W+RM+TM+H+BM; layer = <CALayer: 0x7fb84a462950>>
<UIView: 0x7fb84a463750; frame = (0 468; 320 128); autoresize = RM+BM; layer = <CALayer: 0x7fb84a463530>>
<UIScrollView: 0x7fb84a829000; frame = (0 142; 320 143); clipsToBounds = YES; autoresize = LM+W+RM+TM+H+BM; gestureRecognizers = <NSArray: 0x7fb84a465fa0>; layer = <CALayer: 0x7fb84a4644e0>; contentOffset: {0, 0}; contentSize: {0, 0}>
<UIImageView: 0x7fb84a467560; frame = (310 582.5; 7 2.5); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x7fb84a4672d0>>
<UIImageView: 0x7fb84a467d70; frame = (314.5 578; 2.5 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x7fb84a467ae0>>


Thanks