开发者

How to access subviews in nested UIScrollViews

I was having trouble getting a UIScrollView with multiple UIImageView subviews to zoom properly so I tried using a UIScrollView with multiple UIScrollView subviews inside of it and a UIImageView inside of each of those. That way one UIScrollView can only scroll and the other can only zoom.

My question is: How can I access subvi开发者_开发问答ews of a subview. I know that normally I can access subviews using [scrollView viewWithTag:tagInt]; for example, but I can't seem to access the subviews of a subview using [[scrollView viewWithTag:tagInt] viewWithTag:tagInt2]; since viewWithTag only returns a single UIView and not all of it's subviews.

I could always give each subview a unique tag and access them that way, but that doesn't seem to be the most elegant solution.

What is the best way to access a subView and then get to the subView's subview (ie: access my UIScrollView used for zooming which is a subview of my main view, and then access it's subView UIImageView)?


If you don't feel comfortable subclassing for now, you'd have to assign tags to the UIScrollViews containing images. You'd then have to do what the BobC suggested with a little more work.

for (UIView *subview in [myScrollView subviews])
{
   //check if the current subview is one of the UIScrollViews
   if (subview.tag > 100)
        //do something with the UIScrollView
}


The most elegant way to do this would be to subclass UIScrollView, give it a UIImageView property. That way, you could access this UIImageView with something like:

MyScrollView *myScrollView = (MyScrollView *)[scrollView viewWithTag:tagInt];
UIImageView *imageView = myScrollView.imageView;

Hope this helps!


You can get an array of subviews of any UIView using subviews property.

for(UIView *subview in [myScrollView subviews]) {
     // do anything with your subview here. 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜