UIScrollView content size out of bounds in ios7 - ios

I have a wired bug in iOS7, my UIScrollView content height is out of screen bounds and I can't scroll down to the bottom.
I'm using AutoLayout.
The view heirarchy is:
- UIView
- UIScrollView
- UIView
- Buttons
In iOS8+ it's working fine.
I have this in viewDidLoad:
if ([self respondsToSelector:#selector(automaticallyAdjustsScrollViewInsets)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
And this also:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.scrollView.contentSize = CGSizeMake(320, 1380);
}

Related

UIScrollView can't zoom in iOS7 with Auto Layout

i have created an UIScrollView with this hierarchy (my sample is AutoLayout)
UIViewController
UIView
UIScollView
ContainerView
UIImageView
everything work fine in iOS 8 - 9 expect iOS 7
i can zoom my ImageView in iOS 7
i have implemented :
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
CGSize scrollableSize = CGSizeMake(self.imageView.frame.size.width,self.imageView.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
self.scrollView.backgroundColor = [UIColor whiteColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = 4;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.containerView;
}
i have checked this links :
UIScrollView zooming with Auto Layout
Scroll View not functioning IOS 7
Problem
zoom not work in iOS 7 , ( i need to use Auto Layout ) and my UIImageview should have a AspectFit Ratio
i uploaded my sample on github :
sampleProject

Zooming out from content of UIscrollview

I used to use these two lines to zoom out on the content of my scrollview. However since iOS8, I can't zoom out anymore. It does nothing.
Can someone explain to me why. I have autolayout turned off.
[scrollView setZoomScale:0.3 animated:YES];
scrollView.contentSize = CGSizeMake(tempSize.width*0.3, tempSize.height*0.3);
I am not sure if this will help you, but this method works for me when zooming in iOS 8. In this code, an imageView is in the scroll view.
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.minimumZoomScale = 0.5;
self.scrollView.maximumZoomScale = 6.0;
self.scrollView.contentSize = self.PhotoView.frame.size;
self.scrollView.delegate = self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.PhotoView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView*)view atScale:(CGFloat)scale{
}

UIScrollView setContentSize doesn't work

I have a really big problem with UIScrollView. To make sure it wasn't my actual project, I created a NEW project, iPhone only, for iOS 6 and 7. I disabled autolayout.
I created a Tab Bar project
I created a view controller,
embedded it in a navigation controller and connected it as Tab bar
item #0
I put a UIScrollView in my view.
I connected the scrollview in the custom UIViewController created for this view.
I synthesized my IBOutlet and set the delegate to self. I also
implemented delegate in .h
Here is my code (ViewDidLoad):
self.scrollView.delegate = self;
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)];
NSLog(#"contentSize width %f", self.scrollView.contentSize.width);
NSLog(#"contentSize height %f", self.scrollView.contentSize.height);
NSLog(#"%#", NSStringFromCGAffineTransform(self.scrollView.transform));
It returns me "contentSize width 20000.000000", "contentSize height 0.000000
" and "[1, 0, 0, 1, 0, 0]". It scrolls left-right where it should scroll up-down
Please help!
I had the same issue until I realized I hadn't set the UIScrollView delegate. Also, if you're using auto layout, you need to wait for the layout to be applied to the UIScrollView. This means you should set the Content Size in "viewDidLayoutSubviews".
In viewDidLoad method, frame may not be correct, move the code to this method:
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Your code
}
Use this code. ScrollView setContentSize should be called async in main thread.
Swift:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
DispatchQueue.main.async {
var contentRect = CGRect.zero
for view in self.scrollView.subviews {
contentRect = contentRect.union(view.frame)
}
self.scrollView.contentSize = contentRect.size
}
}
Objective C:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
dispatch_async(dispatch_get_main_queue(), ^ {
CGRect contentRect = CGRectZero;
for(UIView *view in scrollView.subviews)
contentRect = CGRectUnion(contentRect,view.frame);
scrollView.contentSize = contentRect.size;
});
}
Okay, there is absolutely nothing wrong with your code. So some suggestions:
Make sure you added to your .h file like so:
#interface yourViewController : UIViewController <UIScrollViewDelegate> {
}
Try moving this snippet of code to the "viewWillLoad" method:
- (void)viewWillAppear:(BOOL)animated {
self.scrollView.delegate = self;
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)];
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var contentRect = CGRect.zero
for view: UIView in scrollView.subviews {
if !view.isHidden {
contentRect = contentRect.union(view.frame)
}
}
scrollView.contentSize = contentRect.size
scrollView.contentSize.width = self.view.frame.width
}
Working Swift 3.X code. Converted #Karen Hovhannisyan's answer.

Scroll view with Auto Layout

I have a view in scrolling view, and when I seque(push) from this ViewController to CommentViewController, and came back, view in scroll view moved for 200 px up, I dont know why, I don`t anything in code.
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 2000)];
}
Its when Auto Layout is on. When it off, its work, but design is not impressive...
Can I fix it with Auto Layout?
Here's a quick fix
In viewWillDisappear, save the scrollView's contentOffset and reset it to zero.
Later in viewDidLayoutSubview, restore the contentOffset to old value.
Here's the sample code
-(void) viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if(!CGPointEqualToPoint(CGPointZero, self.contentOffset))
{
scroller.contentOffset = self.contentOffset;
self.contentOffset = CGPointZero;
}
}
-(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.contentOffset = scroller.contentOffset;
scroller.contentOffset = CGPointZero;
}

Move a UIView based on the content offset for UIScrollView

I am trying to pull down a UIView (Like a Pull Down Menu) when the user scrolls below a Y-offset of 0.0 in the UITable View. If the user pulls down below -80.0 Y-Offset, then the PullDownMenu locks itself , until the User scrolls the other way.
My implementation for just the UITableView's ScrollView is as follows : [lock:false initially]
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if(isChangingOffset)
return;
if(resetDrag)
{
[self setScrollViewOffset:scrollView offsetTo:CGPointMake(0, -80.0)];
resetDrag = false;
}
float xx = scrollView.contentOffset.y;
NSLog(#"Offset :%f",xx);
if(xx - begginOffset > 0.0 && lock && !doneDragging)
{
offsetChange = xx - begginOffset;
begginOffset = xx;
lock = false;
[self setScrollViewOffset:scrollView offsetTo:CGPointMake(0, -80.0)];
}
if(lock){
[self setScrollViewOffset:scrollView offsetTo:CGPointMake(0, -80.0)];
}
if(xx <=-80.0)
{
[self setScrollViewOffset:scrollView offsetTo:CGPointMake(0, -80.0)];
lock = true;
}
}
- (void)setScrollViewOffset:(UIScrollView *)scrollView offsetTo:(CGPoint)offset{
- (void)setScrollViewOffset:(UIScrollView *)scrollView offsetTo:(CGPoint)offset{
isChangingOffset = true;
scrollView.contentOffset = CGPointMake(0, -80.0);
isChangingOffset = false;
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
float x = scrollView.contentOffset.y;
begginOffset = x;
doneDragging = false;
if(lock){
resetDrag = true;
}
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
doneDragging = true;
}
Here is a working video of how it looks : Video
The blue color is a UIView that I have added as a subview in the UITableView.
My problem is , I am trying to pull down a UIView,lets call it menuView (that is not a subview of the tableView) based on the UITableView's contentOffset. I could have simply added the menuView in the UITableView like I added the blue color view. But that is only accessible via the table, that is when I scroll to the top and drag it down. But I want the menuView to be 'pull-able' like the notification center at any time.
On using the Y-contentOffset of scroll view, the menuview pull down animation is not smooth. And it stops midway or goes too low. It is jerky and not always the same. How can I implement this ?
Here the sample code for your UIScrollView :
ViewController.h
#interface ViewController: UIViewController {
UIScrollView *scrollView;
}
#property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
ViewController.m
#implementation ViewController
#synthesize scrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
[self scrollView];
}
- (void)scrollText{
[scrollView setContentSize:CGSizeMake(320, 800)];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.clipsToBounds = YES;
}
And iside you can put wat you want, from code or interface builder.
For a PullDownMenu you can see this GitHub:
MBPullDownController
Hope this help you and simplify your code ;)

Resources