UIPageViewController改变大小?

时间:2012-09-01 00:24:01

标签: view orientation frame subview uipageviewcontroller

我正在使用Xcode 4中的基于页面的应用程序模板来加载PDF页面并在隐藏文本框上创建UITextView,以便用户可以编写笔记。

到目前为止,我已经完成了所有工作,但是当我添加UITextView时,它在横向模式中处于错误的位置(显示2页)。

// ModelController.m

- (id)init
{
    self = [super init];
    if (self) {
        NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"My PDF File" ofType:@"pdf"];
        NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
        self.pageData = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);  // pageData holds the PDF file
    }
    return self;
}

- (DataViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard
{
    // Return the data view controller for the given index.
    if( CGPDFDocumentGetNumberOfPages( self.pageData ) == 0 || (index >= CGPDFDocumentGetNumberOfPages( self.pageData )))
        return nil;

    // Create a new view controller and pass suitable data.
    DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:@"DataViewController"];
    dataViewController.dataObject = CGPDFDocumentGetPage( self.pageData, index + 1 );   // dataObject holds the page of the PDF file

    [dataViewController view];  // make sure the view is loaded so that all subviews can be accessed

    UITextView  *textView = [[UITextView alloc] initWithFrame:CGRectMake( 10, 20, 30, 40 )];

    textView.layer.borderWidth = 1.0f;
    textView.layer.borderColor = [[UIColor grayColor] CGColor];

    [dataViewController.dataView addSubview:textView];  // dataView is a subview of dataViewController.view in the storyboard/xib

    CGRect  viewFrame = dataViewController.dataView.frame;  // <- *** THIS IS THE WRONG SIZE IN LANDSCAPE ***
}

这种行为让我感到很惊讶,因为当我旋转iPad时没有调用viewControllerAtIndex,所以我无法知道视图框架的实际大小。我在纵向和横向都获得相同的视图框架:

# in Xcode console:
po [dataViewController view]

# result in either orientation:
(id) $4 = 0x0015d160 <UIView: 0x15d160; frame = (0 20; 768 1004); autoresize = RM+BM; layer = <CALayer: 0x15d190>>
#

有没有人知道我是否应该使用转换来正确定位UITextView?我担心我可能必须独立存储元素的位置,并在收到shouldAutorotateToInterfaceOrientation消息后重新定位它们。

似乎Apple可能不正确地实现了UIPageViewController,但我能找到的只是部分解决方法,我仍然试图弄明白:

UIPageViewController and off screen orientation changes

谢谢!

1 个答案:

答案 0 :(得分:3)

我认为这里的技巧是覆盖 DataViewController 中的viewDidLayoutSubviews并管理所有以编程方式插入的非自动调整视图的大小,因为您实际上并不知道父代的内容是什么在那个时间之前做其子视图。

-(void)viewDidLayoutSubviews
{
  [super viewDidLayoutSubviews];

  self.textView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}