content size of UIScrollView - iphone

I have a UIScrollView in which I've set the contentSize to the size of the screen initially. So does this mean that if I have an UIImageView inside the scroll view then it can't zoom bigger than the contentSize I've set?
Because I initially have a UIScrollView with a contentSize of the phone's dimension and when I zoom into an image, it does it just fine (I can see the contentSize grows bigger as I zoom in) but when I adjust it manually it can't. Why is this?
I think when I initially initialize the UIScrollView frame, the content size will be set to the frame's size, however it can grow dynamically as I zoom in. However when I try to set the contentSize, it seems that now it's fixed. The reason why I am asking this is because I have a UIImageView inside a UIScrollView, when I zoom in on the image and I rotate the image, I want the contentSize to reset. Question is how do I reset this?

The zoom level is set by the zoomScale property of the UIScrollView. Its maximum and minimum is set by maximumZoomScale and minimumZoomScale respectively.
If you're trying to set the zoom by setting the contentSize then you're doing it wrong. The contentSize should be set as if the zoom scale was 1.0 and then you'd just set the zoomScale property to be what you wanted so for example 2.0 if you wanted it zoomed in to 200%.
You're probably seeing contentSize change as you zoom because I think the scroll view reports it scaled to the current zoom scale.

Related

UIScrollView zoomToRect vs setZoom

If I have a UIScrollView, and I want to zoom in on a location, is there any difference between just setting the zoom on scrollview like
self.scrollView.zoomScale = MY_NEW_ZOOM;
versus calculating a new rectangle within the zoom to zoom in on, and using
zoomToRect:
Thanks.
zoomScale just sets the zoom scale and doesn't change the content offset.
zoomToRect: will change the zoom scale and the content offset such that the rect you have given will be visible.
Just as a heads up, you may want to ensure that the size of the rect you pass in zoomToRect: is the size of the scrollview's frame. Otherwise the Zoom and Content Offset might not end up where you expect it.

iPhone UIScrollView how to properly zoom the view? Currently it's height is not adjusted properly

I have a UITableView within a UIScrollView. It took me quite a lot of work to make it work.
The tableView is 640x350, I use the scroll view to scroll from one end of the cell to the next.
The scroll view is 320x350.
The scroll view's content size is 640x350
I'm running into this problem:
if I set scrollView's minimum zoom scale to 0.5, the tableview's width now fills the screen, but it's height is only half the screen. I would like the tableview to show more rows when I zoom out to 0.5.
First of all I would like to understand if this is the correct behavior, or the result of my tableView's content size and frame manipulations. The tableview has all springs and struts set in interface builder and should fill the frame available. This is my first attempt at zooming in months, and I don't remember how it works with zooming.
Can someone help me understand where and what do I need to adjust?
As far as I understand, I need to put the code into scrollViewDidZoom: that will manipulate the tableView's frame and content size.
PS. I"m returning the tableview from the viewForZooming: method of UIScrollView
What you are trying to achieve is pretty hard.
Solution 1 This solution uses the exact setup you have (UITableView inside UIScrollView).
You say that when you set the zoomScale to 0.5, you want your table view to fill the scrollView vertically. At 0.5, your table view must be 640x700 in order to fill the UIScrollView as you wish. For this to happen, on scrollViewDidZoom: you must resize the frame of the table view to 640x700
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
// No matter what the zoomScale is, the tableView will be zoomed accordingly
// Only zoom the height of the table
tableView.frame = CGRectMake(0, 0, 640, 350 / zoomScale);
// Also, update the contentSize
scrollView.contentSize = CGSizeMake(640, 350 / zoomScale);
}
If you run the code above for zoomScale = 0.5 you will get a frame size of 640x700.
This only changes the frame of the table and doesn't change the heights of the cells. This means that as you zoom out, you will also see more cells in the tableview.
Solution 2 Use only UITableView
UITableView is a subclass of UIScrollView. This means it has the ability to zoom and scroll around.
Start with a UITableView with the size that you want on the screen. Then, after the content is loaded modify the contentSize and make it wider than your frame width. This should enable horizontal scrolling.
However, UITableViewCells have their frame set automatically to the width of the tableview frame. You can bypass this by using a custom UITableViewCell, with clipsToBounds=false. Inside it you will insert a UIView with the frame set to the width&height you desire and with no autoresizingMask. When the tableview will resize UITableViewCell frame, this will not affect your inner UIView.

UIScrollView - setting proper contentOffset for new contentSize yields undesirable empty space

I have been juggling for a while with UIScrollView now. What I am trying is to insert subviews into the scrollView based on various factors. Since I have scrollview to scroll only in vertical direction, I would insert the subview before or after the current visible view.
So, lets say my current visible view's frames are (0,0,320,480), the contentSize of scrollView is (320,480) and current contentOffset is (0,0)
When I want to insert subview above the currentView and yet keep the currentView in focus, I insert the new subview at the position (0,-480,320,480) and change the contentSize of scrollView to (320, 960) while keeping the contentOffset same as (0,0).
The surprising thing which happens is, UIScrollView adds "extra" space after the currentView instead of inserting it above the currentView. While the newly inserted view above can never be brought into focus coz UIScrollView is assuming the contentSize from a wrong contentOffset perhaps!
I googled and found that there are some others facing similar problems, but did not yield results yet:
Set starting point of content size for UIScrollView
Please let me know if I am doing anything wrong? Or is this some kind of limitation etc?
Thanks!
Edit: Some threads suggests that changing the contentSize will affect contentOffset property, so to make sure that this is not causing problem, I am updating the contentOffset property only after I change the contentSize. Even then I am facing same problems.
You could solve this by :
adding your new subview with frame (0,0,320,480)
setting the frame of your existing subview to (0,480,320,480)
setting the contentSize to (320,960)
setting the content offset to (0,480) - not animated, of course.
So, in effect, moving everything down 480 points
The origin is always (0,0). If you want to insert something above the current offset you'll want to move the current views down by 480 points, add the new view at (0,0) and set the contentOffset to (0,480) (and the contentSize to (320,960).

How to auto-resize UIView proportionally?

I have a UIView whose height should always be exactly 1.5 times the width. When the view is auto-resized (in this case, because the iPhone is rotated), this proportion gets messed up. How can I make sure the height/width ratio doesn't change as the view resizes?
You need to set the views autoresizingMask property to UIViewAutoresizingNone. This will prevent the size of the view from changing at all when the parent view's size changes (such as when the phone rotates.) If you want the view to resize on rotation, but maintain it's aspect ratio (for example if you want it wider in landscape but still 1:1.5), then you will need to set the view's frame yourself to the desired dimensions (but maintaining the desired ratio) in the view controller's willRotateToInterfaceOrientation:duration: method.
You could implement the -sizeThatFits: method for your view to keep the size in proportion.
Alternatively, you could use a custom superview that implements - (void)layoutSubviews to have complete control over the layout.

How much contentOffset changes in UIScrollview for zooming?

In UIScrollview, when I scroll/drag, I got changes in scrollview contentOffset which represent how much I had scrolled/dragged in the scrollview. By this way, I have updated the subview of scrollview for scrolling.
But, when I zoom the scrollview (using pinch zooming), the contentOffsets of the scrollview is also changed. I do not understand how much contentOffset has changed because I can not relate the changes with zoomScale value. So, is there anyway to know changes in contentOffset for zooming?
My intention is get the value of contentOffset changes for dragging while zooming (which is not I am getting because of zooming content offset changes) so that I can update my scrollview's subview accordingly.
I am stuck in here. Any help will be very much appreciated.
thanks
Shaikot
I've set up a small test project, and it looks like the contentOffset is multiplied by the zoomScale. So if you want to account for that, just divide it by the zoomScale before using it.
Maybe it's best to illustrate with a small example:
I've set up a scrollView, and added a view that's big enough to pan around and zoom a bit. To that view, I've added a small red view that I want to keep in the same position, no matter what.
I'm observing the contentOffset property of the scrollView, and I've implemented it like this:
CGPoint contentOffset = self.scrollView.contentOffset;
CGFloat zoomScale = self.scrollView.zoomScale;
self.moveView.frame = CGRectMake((contentOffset.x+10)/zoomScale,
(contentOffset.y+10)/zoomScale,
10,
10);
This keeps the moveView in the same position on the screen, only scaling it when the user zooms.
I hope this clears thing up a bit, but let me know if I can do more to help.