2011-01-14 100 views
0

更改iPadd应用的方向时遇到一些问题。
我写了一个函数,可以设置我的子视图,以便在视图出现或旋转时调用它。
当“viewWillAppear”函数调用该函数时,该函数可以很好地工作。但是,当“willAnimateRotationToInterfaceOrientation”函数调用我的布局功能(“setPositionsForOrientation”)我现在面临以下问题:在iPad上更改方向后错误定位

  • 当从纵向改为横向我有一个128像素左侧
  • 偏移时更改风景人像我有一个负左侧

在我的印象偏移,莫明的新的帧的属性没有被正确处理,因为我的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 

我做了什么错,更重要的是,我怎么能解决它显示

风景模式?
我发现this帖子里面介绍了常用的方式,但我不明白我怎么可以重写我的意见“layoutSubviews”的方法,因为视图仅仅是我的UIViewController财产..

回答

0

如果你想创建你拥有UIView的子类并将其设置为UIViewController,这样你就可以实现layoutSubviews方法了,其实很简单!

UIViewController不会创建一个视图,但它期望用户设置视图,但在使用Interface BUilder时有一个UIView已经为我们设置。

要更改视图做:

  • 创建你的子类,在这里我将假设它被称为MyView的,并把那里所有的UI的东西(工具栏,按钮等)。
  • 在IB打开视图控制器文件(*的.xib)
  • 选择视图(我们正在改变视图)
  • 转到身份检查(或者按CMD + 4)
  • 输入“ MyView“在类标识字段

既然你把所有的UI元素放在视图中作为伊娃,你不能从你的UIViewController中访问它们吧?所以,确保你有正确的访问器/属性和方法来处理来自UIViewController的视图(和它的ivars)。;)

alt text

好运;)

编辑: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" 

这应该工作。但我不明白为什么那么有:“属性‘视图’类型不匹配的超类的UIViewController的“属性类型”第一次尝试了我刚才说的,)你确定你MyView的声明为:

@interface MyView: UIView{ 
... 
} 
@property (...) XYZ *xyz; 
... 
@end 
+0

我做的一切你告诉我,感动了所有实例变量和属性,以新的视图类。 但是,对于所有属性,我都会收到错误“请求会员不属于结构或联盟”。 如果我用我的新类重新声明视图控制器中的视图属性,但是然后我得到警告“属性”视图“类型不符合超类'UIViewController'属性类型”,并且应用程序崩溃时,此错误消失显示控制器。 :( – 2011-01-17 10:56:27

0

我在.m文件的MyView的类添加以下代码,并帮我走出我的痛苦:

- (void)awakeFromNib 
{ 
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
}