Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ipad UIScrollView在方向更改时未获得新大小_Ipad_Uiscrollview_Orientation_Ios - Fatal编程技术网

Ipad UIScrollView在方向更改时未获得新大小

Ipad UIScrollView在方向更改时未获得新大小,ipad,uiscrollview,orientation,ios,Ipad,Uiscrollview,Orientation,Ios,我有一个非常简单的UIScrollView示例,它根本不做它应该做的事情。 不确定这是API的错误还是代码中的错误 基本上,我有一个UIViewController,它的视图是UIScrollView。 当我将其添加到ui窗口并更改iPad的方向时,我会注销UIScrollViews大小,这是错误的(?)报告 下面是我的UIViewController实现: @implementation CustomViewController - (void)loadView { scrollView

我有一个非常简单的
UIScrollView
示例,它根本不做它应该做的事情。 不确定这是API的错误还是代码中的错误

基本上,我有一个
UIViewController
,它的视图是
UIScrollView
。 当我将其添加到
ui窗口
并更改iPad的方向时,我会注销
UIScrollView
s大小,这是错误的(?)报告

下面是我的
UIViewController
实现:

@implementation CustomViewController
- (void)loadView {
  scrollView = [[[UIScrollView alloc] init] autorelease];
  scrollView.delegate = self;
  scrollView.backgroundColor = [UIColor redColor];
  self.view = scrollView;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  CGSize rect = scrollView.frame.size;
  NSLog(@"will rotate w%f h%f", rect.width, rect.height);
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  CGSize rect = scrollView.frame.size;
  NSLog(@"will animate rotation w%f h%f", rect.width, rect.height);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
  CGSize rect = scrollView.frame.size;
  NSLog(@"did rotate w%f h%f", rect.width, rect.height);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return YES;
}

@end
运行上述代码时,我在控制台中看到以下条目:

2010-07-11 11:03:05.214 Untitled2[6682:207] will rotate w768.000000 h1004.000000
2010-07-11 11:03:05.214 Untitled2[6682:207] will animate rotation w748.000000 h1024.000000
2010-07-11 11:03:05.619 Untitled2[6682:207] did rotate w748.000000 h1024.000000
2010-07-11 11:03:07.951 Untitled2[6682:207] will rotate w748.000000 h1024.000000
2010-07-11 11:03:07.958 Untitled2[6682:207] will animate rotation w768.000000 h1004.000000
2010-07-11 11:03:08.367 Untitled2[6682:207] did rotate w768.000000 h1004.000000
如您所见,方向更改确实会调整
UIScrollView
的大小,但仅允许使用新的状态栏。 我预计宽度和高度会发生剧烈变化,因为
UIScrollView
的宽度比高度大,反之亦然


有没有办法让
UIScrollView
报告它的实际大小?

视图在超级视图调整大小时不会自动调整大小,除非您专门将其设置为。对于该滚动视图,当superview调整其宽度和高度时,您需要告诉它调整其宽度和高度

scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

我注意到,如果一开始使用iPhone NIB文件而不是专用的iPad NIB文件,那么在
didRotateFromInterfaceOrientation
中确定新的
contentSize
也是不可能的:iPhone NIB文件中的帧要小得多,因此计算出的内容高度要比应该的大得多当你真的在iPad上的时候。苹果确实说过我必须创建一个单独的iPad NIB文件

因此,我确实在
didRotateFromInterfaceOrientation
中得到了旧的维度,但对我来说,现在问题不大了,因为一旦我开始使用正确的NIB文件,iOS就可以处理所有问题。尽管如此,在
didRotateFromInterfaceOrientation
方法中,帧尺寸对于当前视图来说是不正确的,这对我来说似乎不太直观

最后,我确实找到了一个解决方案,只需使用通知,而不是
didRotateFromInterfaceOrientation

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(orientationChanged:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];

然后在
orientationChanged
方法中,根据新帧调整视图。我还有最后一个问题:如果初始方向与nib中的方向不同,视图仍然是错误的,我通过创建一个新的nib文件来解决这个问题,特别是针对横向视图。在init方法中,
ViewController
根据当前方向选择加载哪个NIB。

我通常通过将
contentSize
的一个值设置为0来解决这个问题,就像水平视图的
CGSizeMake(contentSize.width,0)
一样

强制
UIScrollView
仅滚动,例如将
contentSize
的不可滚动值水平设置为0>
CGSizeMake(contentSize.width,0)


我希望这会有帮助。

如果你还在找,我有答案。测试边界,而不是帧


原因:frame是边界的派生属性,用于旋转的变换。

我不知道它对您有什么帮助,因为我从未测试过它,但我认为它可能对您有帮助

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
self.view = scrollView;
CGSize rect = scrollView.frame.size;
NSLog(@"will rotate w%f h%f", rect.width, rect.height);
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
self.view = scrollView;    
CGSize rect = scrollView.frame.size;
NSLog(@"will animate rotation w%f h%f", rect.width, rect.height);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
 self.view = scrollView;
 CGSize rect = scrollView.frame.size;
 NSLog(@"did rotate w%f h%f", rect.width, rect.height);
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
 }  

不幸的是,在添加了一个调整大小的掩码之后,我仍然得到了无效的维度。视觉上一切都很好,但我不能依赖于框架尺寸。我通过检测设备方向并在横向移动时翻转宽度和高度来解决这个问题-这并不理想,但它可以工作。您需要测试[[UIDevice currentDevice]方向]并查看以下内容:UIDeviceOrientationisPortrait()或类似的内容来测试它。框架将出错,但它将报告正确的方向。这很烦人,我现在正在处理这件事,但很高兴有些事情是可靠的。也许边界答案可以帮助我自动调整UIScrollView的大小,但即使手动更改数学以匹配方向,我的UIScrollView仍不正常。这不起作用。我截取一个更改,然后使用NSStringFromRect(self.bounds)并打印“更改前”的边界。但是currentDevice方向返回正确的值。至少一个人总是正确地返回相同的东西,但是人。。。这个问题不应该花这么长时间。从文档中,虽然边界是未派生的边界,但它不是始终为0,0,与帧宽度相同,与帧高度相同吗?默认情况下,边界原点始终为0,0,除非您更改要显示的视图部分。这仅仅是故事的一半,CGRect也包含一个大小成员,当您调整视图的大小和方向时,这通常会改变。上面的“will”函数提供“before value”,而“did”函数提供“after value”。