ViewControllers with TextViews in UIPageViewController
ios6, objective-c, uipageviewcontroller, uitextview, uiviewcontroller
Solution
I've investigated a lot on this problem. It seems a bug related to the internal (private) UIScrollView of the UIPageViewController. If you search on StackOverflow you will find a lot of post with this problem and no solutions...
I seems that the UITextView (which is an UIScrollView and, AFAIR, has an internal UIWebView), sends some strange message to it's superviews chain, that makes the private UIScrollView of the UIPageViewController scrolling to the top-left corner.
I would have tried to block this message using method swizzling, but this is probably not ok for AppStore. So I tried other things.
The final solution is very simple: simply, embed your UITextView inside an UIScrollView!
This is a link to your project updated
If you do so, you'll solve the problem!
Try and let me know
EDIT:
How did I arrive to this solution:
An intuition.
A lot of debug and stack traces had make me think that the problem was related to a bug in the "nesting UIScrollView" system and some messages sent from the inner view to its superview.
UITextView inherits from UIScrollView and has inside an UIWebDocumentView (private) which is another UIScrollView. During the debug I saw a lot of messages (private methods) like "relayout superview" sent to the upper UIScrollView's. So, for some reason, the inner scroll view (UIWebDocumentView?) was sending a message/event to it's superview. This message/event (probably because of a bug) was not stopping to the external UITextView, and was forwarded to the UIScrollView handled by UIPageViewController.
Embedding the UITextView inside a simple UIView was not enough, because UIView forward the message to it's superview if it can't handle. I thought: UIScrollView probably doesn't (otherwise it wouldn't simple to nest UIScrollViews), so I tried and it worked.
This is all a supposition because I stopped inspecting, I will have a more in-depth look this week.
Problem
I was trying to learn `UIPageViewController`s and hit an Issue which I couldn't resolve. This is what I tried to do: - Steps: - I simply created 2 view controllers and a page view controller in StoryBoard. - Then I added some code to the File's Owner of `PageViewController` to behave as a `dataSource` and `delegate` to itself. - When I ran, things worked well. - I added some buttons, and text fields to the second view controller. - I ran, worked well. - Now I added a text view to the second view controller and ran. When I tried to write something inside the text view, the page control jittered and moved to first view controller. Has anyone experience this ever? ``` @interface AMPageViewController : UIPageViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate> @end ``` The implementation: ``` #import "AMPageViewController.h" @interface AMPageViewController () { UIViewController *mainController; UIViewController* socController; } @end @implementation AMPageViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; mainController = (UIViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"First"]; socController = (UIViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"Second"]; [self setViewControllers:@[mainController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; self.dataSource = self; self.delegate = self; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { if (viewController == socController ) return mainController; else return nil; } - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { if (viewController == mainController ) return socController; else return nil; } - (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController { return 2; } - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController { return 0; } @end ``` If you want to download and try the project