UIScrollView doesn't scroll after upgrading to

2019-01-11 07:24发布

问题:

I have an app which in xcode 4.5 and ios 6.1 worked perfectly fine when scrolling. However, after downloading xcode 5 and iOS 7 my scroll views does not work anymore.???

Here is my .h file:

#import <UIKit/UIKit.h>

@interface GrillretterViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIScrollView *grillretterScroller;
@property (assign) CGPoint rememberContentOffset;

@end

And here is my .m file:

#import "GrillretterViewController.h"

@interface GrillretterViewController ()

@end

@implementation GrillretterViewController
@synthesize grillretterScroller, rememberContentOffset;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [grillretterScroller setScrollEnabled:YES];

    // Do any additional setup after loading the view.
}

- (void) viewDidAppear:(BOOL)animated {
    [grillretterScroller setContentSize:CGSizeMake(300, 915)];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.grillretterScroller.contentOffset = CGPointMake(0, 0);
}

- (void)viewWillDisappear:(BOOL)animated {
    self.rememberContentOffset = self.grillretterScroller.contentOffset;
    [super viewWillDisappear:animated];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.grillretterScroller.contentOffset = CGPointMake(0, self.rememberContentOffset.y);
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

PLEASE help! I'm stuck!

Best regards!

回答1:

I solved this by deselecting 'Use Autolayout' in the File Inspector pane of main view within the Scroll View.

If you want to keep 'Autolayout' enabled, try 'Editor -> Reslove Autolayout Issues -> Add Missing Constraints'. The key constraint appears to be 'Bottom Space to: Superview and in my case was -300, giving 300 scroll space on the botton of the view.



回答2:

As mentioned by user2394787:

Select your view controller that has the scrollview then go to

Editor -> Resolve AutoLayout Issues -> Add Missing Contraints in [your view controller].  

This will make it work.

I tried to vote up the above answer but do not have enough rep.



回答3:

I just solved it by going to Editor > Resolve AutoLayout Issues > Add Missing Constraints in View Controller

Hope this helps.



回答4:

I just set "Vertical Space" to auto constant value. May be it help in some case.

and set it in Attributes inspector

I hope it help. Thx.



回答5:

I'm not sure if this is going to work

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [grillretterScroller setContentSize:CGSizeMake(self.view.bounds.size.width, 915)];
}


回答6:

- (void)viewDidLayoutSubviews {
    [self performSelector:@selector(updateContentSize)
               withObject:nil
               afterDelay:0.25];

}

-(void)updateContentSize{
    UIView *viewLast = [viewContent viewWithTag:100];
    scrollViewAd.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, CGRectGetMaxY(viewLast.frame));
}


回答7:

I didn't have a fixed height so I set the contentsize of the scrollview in code according to my label inside the scrollview

CGSize scrollViewContentSize = CGSizeMake(320, myLabel.frame.size.height);
[self.scrollView setContentSize:scrollViewContentSize];


回答8:

With AutoLayout enabled, make sure Vertical Spacing (@"V:|-[subview]-|") is applied between the UIScrollView and its subview. If your scrollView is for horizontal scrolling, apply Horizontal Spacing (@"H:|-[subview]-|").



回答9:

I had the same issue after developing an app for iOS8, then testing on iOS7 afterwards. Although I had tried setting certain options on the scroll view such as:

[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 700)];
[self.scrollView setContentOffset:CGPointZero];

The solution that worked for me was to go into the storyboard file, and ensure that you have constraints set for all views within the scroll view. The scroll view's content size is calculated by the constraints of each subview within the scroll view, so in my case, I had a UIView and UITableView. After setting constraints for UIView (top, leading, trailing, height), and UITableView (vertical spacing to UIView, leading, trailing, bottom), the scroll view calculated the content size and started to function.

While turning off Auto Layout does work, it's not an ideal solution for everyone. Just play around with Storyboard and set constraints for all subviews in the scroll view, so that a vertical line of constraints are set, from the top of the scroll view to the bottom.