我整个下午都被这个问题困扰了。 我做了一个自定义的UIViewController。到目前为止,我在此自定义viewcontroller中显示了自定义视图。一切都很好。现在我想要做的是在自定义控制器中显示一个pageviewcontroller。没有错误,但是页面控制器的视图显示在自定义控制器的边界之外(。
这是我的代码
- (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;
这是界面(非常简单):
@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
我尝试(如第一个答案所示)在最后调用didMoveToParentViewController:
CGRect pageViewRect = self.view.bounds;
pageViewRect = CGRectInset(pageViewRect, 40.0, 40.0);
self.pager.view.frame = pageViewRect;
[self.pager didMoveToParentViewController:self];
但如图所示没有运气:
自定义视图控制器位于日历视图下方。日历视图应位于自定义视图控制器内,而不是外部。 而且,我怎样才能将脊柱放在顶部?
[解决] 没关系:第一个答案是对的。我的错误是错误的框架集。
答案 0 :(得分:2)
我遇到了完全相同的问题。在调用didMoveToParentViewController:
之前设置页面视图控制器的框架应解决它。