更改我的 iPadd 应用程序的方向时遇到一些问题。
我编写了一个函数来布置我的子视图,以便在视图出现或旋转时调用它。
该函数在由“viewWillAppear”函数调用时可以完美运行。但是当“willAnimateRotationToInterfaceOrientation”函数调用我的布局函数(“setPositionsForOrientation”)时,我面临以下问题:
- 从纵向更改为横向时,左侧有 128px 偏移
- 从横向更改为纵向时,左侧有一个负偏移
我的印象是,不知何故新的框架属性没有正确处理,因为我的 UIScrollViews 也应该调整大小,但他们不这样做。
这就是我写的代码:
- (void)viewWillAppear:(BOOL)animated{
[self setPositionsForOrientation:self.interfaceOrientation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)newInterfaceOrientation duration:(NSTimeInterval)duration {
[self setPositionsForOrientation:newInterfaceOrientation];
}
- (void)setPositionsForOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Position the UI elements for landscape mode
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
self.view.frame = CGRectMake(0, 0, 1024, 748);
self.menubarImage.frame = CGRectMake(0, 0, 1024, 121);
self.backgroundImage.frame = CGRectMake(0, 0, 1024, 748);
self.mainCategories.frame = CGRectMake(6, 6, 1012, 55);
self.subCategories.frame = CGRectMake(58, 79, 960, 30);
self.moviesContainer.frame = CGRectMake(6, 204, 1012, 456);
self.searchButton.frame = CGRectMake(935, 709, 80, 30);
}
// Position the UI elements for portrait mode
else {
self.view.frame = CGRectMake(0, 0, 768, 1004);
self.menubarImage.frame = CGRectMake(0, 256, 768, 121);
self.toolbarImage.frame = CGRectMake(0, 924, 768, 80);
self.backgroundImage.frame = CGRectMake(0, 256, 768, 748);
self.mainCategories.frame = CGRectMake(6, 262, 756, 55);
self.subCategories.frame = CGRectMake(58, 335, 704, 30);
self.moviesContainer.frame = CGRectMake(6, 460, 756, 456);
self.searchButton.frame = CGRectMake(680, 963, 80, 30);
}
}
这里有一些图片来说明我的问题..
IB 中的布局(所有子视图的 contentMode 设置为左上角)
http://www.abload.de/image.php?img=interfacebuilderl9ey.png
人像模式显示正确/奇怪
http://www.abload.de/image.php?img=portrait_oklxf1.png
http://www.abload.de/image.php?img=portrait_failma8y.png
横向模式显示正确/奇怪
http://www.abload.de/image.php?img=landscape_ok4z2l.png
http://www.abload.de/image.php?img=landscape_failvzuy.png
我做错了什么,更重要的是,我该如何解决?
我发现这篇文章描述了通常的方式,但我不知道如何覆盖我的视图“layoutSubviews”方法,因为视图只是我的 UIViewController 的一个属性。