Getting the visible rect of an UIScrollView's content

Kenneth Ballenegger picture Kenneth Ballenegger · May 15, 2009 · Viewed 68.9k times · Source

How can I go about finding out the rect (CGRect) of the content of a displayed view that is actually visible on screen.

myScrollView.bounds

The code above works when there's no zooming, but as soon as you allow zooming, it breaks at zoom scales other than 1.

To clarify, I want a CGRect that contains the visible area of the scroll view's content, relative to the content. (ie. if it's a zoom scale 2, the rect's size will be half of the scroll view's size, if it's at zoom scale 0.5, it'll be double.)

Answer

Trenskow picture Trenskow · Jul 3, 2012

Or you could simply do

CGRect visibleRect = [scrollView convertRect:scrollView.bounds toView:zoomedSubview];

Swift

let visibleRect = scrollView.convert(scrollView.bounds, to: zoomedSubview)