I have got a UIScrollView with a UIImageView as subview. The contentSize of the ScrollView has been set to the size of the UIImageView.
I implemented zooming and scrolling of the UIImageView.
I need the UIScrollView to keep the scroll rect when I change its frame (proportionally).
How is this done?
My problem is, whenever I change the frame of the UIScrollView the visible rect of the UIImageView changes.
Thanks!
If I understand your problem correctly, you want to shrink the frame of the scroll view without changing what you are able to see inside of the scroll view. You can do this in the following way:
Create a new UIView and set its frame to the position and size that you want to see the content of the scroll view. Turn on the clips to bounds on that view, view.clipsToBounds = yes. Add the scroll view as a subview to the view you created. [view addSubview:scrollview]. Turn clips to bounds off on the scrollview, scrollview.clipsToBounds = no.
Hope that helps.
Related
I have a UIScrollView for which I have a UIView which is the subview of the scroll view , the UIView has a lot of other subviews and I am getting the height for it dynamically after adding the subviews , this is my piece of code to add the view to scroll view
CGRect frameOfView = CGRectMake(0, 0,Get_Bounds.width, globalYPosition);
self.parentProductDetailView = [[UIView alloc]initWithFrame:frameOfView];
I am first initialising the view this way and then after adding all subviews I am doing this,
frameOfView.size.height = globalYPosition;
[self.parentProductDetailView layoutSubviews];
self.parentProductDetailView.frame = frameOfView;
[self.productDetailScrollView addSubview:self.parentProductDetailView];
self.productDetailScrollView.contentSize = CGSizeMake(0, self.parentProductDetailView.frame.size.height *1);
But my scrollview does not scroll properly it either sticks to top or bottom.
Here globalYPosition is the sum of height of all subviews added to parentProductDetailView
The procedure you used seems correct. No matter the subviews your scroll view should scroll properly by simply using a larger content size then its frame size.
The scroll view may stick, snap to some points if paging is enabled which is what is happening in your case. If the content view is larger then 1.5th of the frame size then the scroll view will snap to top/bottom or left/right. If it is smaller then it will only snap to starting position.
This may be very useful for situations like having a side menu that takes a part of a screen but in your case you should simply disable the paging and scrolling should work fine.
In my app, I need to add subview in another view but my issue is that height and width of parentView is 50*50 and height of subview is 150*150 so how can I add this? because when I add this view, rest portion of subview is getting cropped.
The edges are bounded in the smaller UIView. Because the UIView is smaller it is going to crop out the larger subview. if you still want the super view to be 50x50 and don't want the larger subview cropped out then turn off setClipsToBound property of the UIView:
Swift: view.setClipsToBound = false
Objective-C: [view setClipsToBound:NO];
Another solution is just to make the subview smaller or make the superview containing the large subview larger.
I have an image as a subview of a UIScrollView. I want the image to initially fit the screen's bounds using autoResizingMask and contentMode (UIViewContentModeAspectFit) property of UIView. When the scrollview's frame is changed to make room for the keyboard, I don't want the child image view to scale down to fit the smaller frame. I can't disable autoResizeSubviews on the scroll view when it is created because the child view must be re-sized once at the beginning. Right now I can turn off subview re-sizing when the keyboard appears and re-enable it when it dissapears. This seems to work fine but seems hackish. Is that an acceptable way to do it or is there a better solution? Thanks.
Don't worry about the autoResizingMask since it's a subview of the UIScrollView.
The autoResizingMask on a UIView allows for the view to be automatically resized when its parent view's frame has been resized. In this scenario, it sounds like your scrollView's frame is being adjusted vertically to accommodate the keyboard's on screen frame. When the parent view's frame shrinks, so does your UIImageView, which means it works as designed if you're using UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight as the autoResizingMask of your UIImageView.
Instead of using autoResizingMask on your UIImageView, you should manually set its frame to be the same size as the UIScrollView's frame via the bounds property:
imageView.frame = scrollView.bounds;
Then let's set the scrollView's contentSize to be the same size as the imageView.
scrollView.contentSize = imageView.frame.size;
This way, your imageView should be the full size of your scrollView, and won't move if you adjust the size of the scrollView frame.
I config scrollView contentSize same as its frame height(I also test <), Why scorll bar still shown, it seems that the scrollview content size is not what I configed in code.
I think you are place scroll view in view with size for Retina 4 inch, and scroll view automaticaly resized.
add line in viewDidLoad method
self.mainScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
or set up right autoresizing mask in IB.
I have a UIView which I need to stretch to the width of a UIScrollView. The problem is the next:
I'd need to stretch that UIView (which is EGOTableViewPullRefresh, just a custom UIView). In the view initialization I put [_refreshHeaderView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];. I tried:
-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
NSLog(#"scrollViewDidZoom");
[_refreshHeaderView setFrame:CGRectMake(0.0f, 0.0f - webScroller.bounds.size.height, webScroller.frame.size.width, webScroller.bounds.size.height)];
}
But it just looks as the image. If I enter to a iOS designed page (such as Mobile Google), it looks ok:
How can I do that?
The size.width of a UIScrollView is width of the view on your device, not the width of the contents inside the scroll view (which would be scrollView.contentSize.width).
This means, if "your view" is outside the UIScrollView you do want the scroll view width, but if your view is inside the scroll view you need the content width.
Notice the bottom/Google screenshot you provided. Notice how there is no horizontal scrolling? In this case the scroll view contents size width is the same as the scroll view width so it works perfectly. The upper/stack overflow image does have a horizontal scroll bar though. So the content width of the scroll view is bigger than the scroll view width.
Short answer: Try setting your view to be the scrollView.contentSize.width, not scrollView.frame.size.width