我使用 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 没有被调用,所以我无法知道 View 框架的实际大小是多少。我在纵向和横向上得到相同的 View 框架:
# 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
谢谢!
最佳答案
我认为这里的技巧是重写 DataViewController 中的 viewDidLayoutSubviews 并管理所有以编程方式插入的非自动调整大小 View 的大小,因为您并不真正知道父级将要做什么直到那时对其 subview 执行操作。
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.textView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
关于view - UIPageViewController 改变大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12223932/