UIPageViewController inside a custom UIViewController

▼魔方 西西 提交于 2019-12-21 21:35:40

问题


I am stucked with this problem for all the afternoon. I made a custom UIViewController. Up to now I shown custom views inside this custom viewcontroller. All fine. Now what I want to do is to show a pageviewcontroller inside the custom controller. No error, but the view of the pagecontroller is shown outside the bounds of the custom controller (.

Here is my code

- (void)viewDidLoad
{
[super viewDidLoad];
self.monthYearController = [[avvAgendaMonthViewController alloc] init];
self.yearViewController  = [[avvAgendaYearViewController alloc] init];
self.pager               = [[UIPageViewController alloc]   initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
  navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                            options:nil];

//Assign the delegate and datasource .
self.pager.delegate = self;
self.pager.dataSource = self;

//Set the initial view controllers.
NSArray *viewControllers = [NSArray arrayWithObject:self.yearViewController];
[self.pager setViewControllers:viewControllers
            direction:UIPageViewControllerNavigationDirectionForward
            animated:NO
            completion:nil];



//Add the pageViewController as the childViewController
[self addChildViewController:self.pager];
[self.view addSubview:self.pager.view];

[self.pager didMoveToParentViewController:self];
CGRect pageViewRect = self.view.bounds;
pageViewRect = CGRectInset(pageViewRect, 40.0, 40.0);
self.pager.view.frame = pageViewRect;


//Assign the gestureRecognizers property of our pageViewController to our view's gestureRecognizers property.
self.view.gestureRecognizers = self.pager.gestureRecognizers;

And here is the interface (very simple):

@interface CustomNavigatorViewController : UIViewController      <CustomNavigationDelegate,UIPageViewControllerDataSource,UIPageViewControllerDelegate>

@property (nonatomic,strong) CustomNavigation* navigation;
@property (nonatomic,strong) UIPageViewController *pager;
@property (nonatomic,strong) AgendaYearViewController* yearViewController;
@property (nonatomic,strong) AgendaMonthViewController* monthYearController;
@property (nonatomic,strong) UIViewController* agendaCurrentView;


@end

I tried (as suggested in the first answer) to call didMoveToParentViewController at the end:

CGRect pageViewRect = self.view.bounds;
pageViewRect = CGRectInset(pageViewRect, 40.0, 40.0);
self.pager.view.frame = pageViewRect;
[self.pager didMoveToParentViewController:self];

but no luck as the picture shows:

the custom view controller is underneath the calendar view. The calendar view should be inside the custom view controller and not outside. Moreover, how can I put the spine on the top?

[SOLVED] Never mind: the first answer was right. My mistake was a wrong frame set.


回答1:


I had the exact same problem. Setting the frame of the page view controller before calling didMoveToParentViewController: should resolve it.



来源:https://stackoverflow.com/questions/17597051/uipageviewcontroller-inside-a-custom-uiviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!