0

更改我的 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 的一个属性。

4

2 回答 2

0

如果您想创建自己的 UIView 子类并将其设置为 UIViewController 以便您可以实现 layoutSubviews 方法,实际上非常容易!

UIViewController 不会创建它希望用户设置视图的视图,但是当使用 Interface BUilder 时,已经为我们设置了一个 UIView。

要更改视图,请执行以下操作:

  • 创建您的子类,在这里我假设它被称为 MyView,并将您的所有 UI 内容(工具栏、按钮等)放在那里。
  • 在 IB 中打开您的视图控制器文件(*.xib)
  • 选择视图(我们正在更改的视图)
  • 转到身份检查器(或按 Cmd+4)
  • 在班级标识字段中输入“MyView”

由于您将所有 UI 元素作为 ivar 放在视图中,因此您不能再从 UIViewController 访问它们了,对吧?因此,请确保您有正确的访问器/属性和方法来处理来自 UIViewController 的视图(及其 ivars)。;)

替代文字

祝你好运 ;)

编辑:2011/01/17

当您在控制器中并执行 [self.view xyz] 时,您会收到警告吗?那是因为 UIViewController 的视图被声明为 UIView 对象。你将 UIView 子类化并在 IB 中设置它。因此,每次访问视图时都必须强制转换它,否则编译器会认为它仍然是正常的 UIView,它没有您在 MyView 中声明的方法。这样做:

[(MyView *)self.view xyz]; //and for that don't forget to #import "MyView.h"

这应该有效。但我不明白为什么然后得到:“属性'view'类型与超类'UIViewController'属性类型不匹配”首先尝试我刚才所说的;)你确定你将MyView声明为:

@interface MyView: UIView{
...
}
@property (...) XYZ *xyz;
...
@end
于 2011-01-14T16:09:26.650 回答
0

我在 .m 文件的 MyView 类中添加了以下代码,它帮助我摆脱了痛苦:

- (void)awakeFromNib
{
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
于 2011-01-26T08:51:47.303 回答