2

UPDATED -- I have an iPad app that was originally designed and written for portrait mode only; I now want to add a UIScrollView so it will scroll in landscape mode. Auto Layout is checked and the different scenes are built using Storyboard. (I am following this tutorial). The major problem is when switching from portrait to landscape, the bounds of the frame change drastically, thereby causing problems with the logic of the scroll.

This is the image of the first scene (UIView) I am trying to add a UIScrollView to: enter image description here

This is what it looks like in landscape mode (w/o scrolling): enter image description here

This is my code in the -viewDidLoad method for that scene:

//  create UIScrollView
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,self.bookDetailView.frame.size.width, self.bookDetailView.frame.size.height)];
scroll.delegate = self;
scroll.pagingEnabled = YES;
scroll.scrollEnabled = YES;
scroll.showsVerticalScrollIndicator = YES;
CGSize scrollableSize = CGSizeMake(768, 1024);  //  size of portrait mode
scroll.contentSize = scrollableSize;

[self.view addSubview: scroll];

The scroll bar (as thin as it is) now shows, BUT although it moves like it should, the UIView doesn't move. Portrait mode works fine (no scrolling needed) but landscape mode doesn't scroll at all (even tho' the vertical scroll bar does move). I'm wondrering if I should abandon the idea of using scrolling for landscape mode and create separate scenes for landscape mode instead.

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120

1 Answers1

4

Is there any reason why you need a separate UIScrollView ? UITableView is already a scroll view. My guess is that the touch events from the UIScrollView you created are interfering with those of the UITableView.

Abel Duarte
  • 353
  • 1
  • 9
  • So really it wasn't originally a UITableView, you just want your already existing content to scroll ? If so then what you need to do is grab is add your content view into a UIScrollView. You then need to set the UIScrollViews content size for it to know how far to let users scroll. This might be why your scroll view was not scrolling. – Abel Duarte Nov 23 '15 at 22:13
  • UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:...]; [myScrollView addSubview:contentView]; myScrollView.contentSize = CGMakeSize(width, height); – Abel Duarte Nov 23 '15 at 22:15
  • Also, [self.bookDetailView addSubview:scroll]; this is the opposite of what you need to do. You need to add the bookDetailView into the scroll view not the other way around. This still won't scroll until you set the scroll view's contentSize as pointed out above. You can simply get the bounds of the bookDetailView and use that to get the width and height for the contentSize. – Abel Duarte Nov 23 '15 at 22:17
  • Is scrollingEnabled set to true ? – Abel Duarte Nov 23 '15 at 22:24
  • Also make sure that you add the scroll view to the view controller. You can't just simply add the bookDetailView into the scroll view without adding the scroll view into your view controller first and foremost. – Abel Duarte Nov 23 '15 at 22:24