UIScrollView doesn't scroll in the right direc

2019-08-31 06:43发布

问题:

I got a weird bug. I have a UIViewController with a UIScrollView embedded inside the UIView. Inside the UIScrollview are few images, but I need more space, so I want to put some images under the current ones. I did all the usual stuff to create and initialize my ScrollView, but instead of scrolling in the UP/DOWN direction, it scrolls LEFT/RIGHT … What is going on?

  • I declared "UIScrollViewDelegate" in the .h
  • My property @property (weak, nonatomic) IBOutlet UIScrollView *scrollViewContent; is connected to my scrollview in IB

  • My ContentSize is declared as: self.scrollViewContent.contentSize= CGSizeMake(self.scrollViewContent.frame.size.width, 1000); and whether I declare it in ViewDidLoad or viewDidLayoutSubviews doesn't change anything.

  • I set self.scrollViewContent.delegate = self; in ViewDidLoad

UPDATE

Interestingly enough, I tried setting the ContentSize and then printing it to the console. As it turns out, the "Height" attribute always returns 0. Also, note that I set the "Width" to 1500, but it prints 2000. I'm not using auto-layout.

 [self.scrollViewContent setContentSize:CGSizeMake(1500, 2000)];

    NSLog(@"contentSize width %f", self.scrollViewContent.contentSize.width);
    NSLog(@"contentSize height %f", self.scrollViewContent.contentSize.height);

Result:

"contentSize width 2000.000000" "contentSize height 0.000000"

UPDATE 2 If I take the exact same Code and the exact same Xib in another project, it works just fine… what is wrong with my actual project?

回答1:

Please cross check that you declared

UIScrollViewDelegate at .h file or not..

At .m File

@synthesize scrollViewContent;

Hope it may help you...