iPad 上方向改变后定位错误

发布于 2024-10-12 01:03:52 字数 2707 浏览 4 评论 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 的属性..

Having some problems when changing the orientation of my iPadd app.
I wrote a function that lays out my subviews so I can call it when the view appears or is rotated.
The function works perfectly when it is called by the "viewWillAppear" function. But when the "willAnimateRotationToInterfaceOrientation" function calls my layout function ("setPositionsForOrientation") I'm facing following issues:

  • When changing from portrait to landscape I have a 128px offset on the left side
  • When changing from landscape to portrait I have a negative offset on the left side

I have the impression, that somehow the new frame-properties are not handled correctly, as my UIScrollViews should also resize, but they don't do it.

That's the code I wrote:

- (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);
 }
}

And here are some pictures to illustrate my problem..

Layout in IB (All subviews have contentMode set to Top-Left)

http://www.abload.de/image.php?img=interfacebuilderl9ey.png 

Portrait mode shown correctly / strange

http://www.abload.de/image.php?img=portrait_oklxf1.png
http://www.abload.de/image.php?img=portrait_failma8y.png

Landscape mode shown correctly / strange

http://www.abload.de/image.php?img=landscape_ok4z2l.png
http://www.abload.de/image.php?img=landscape_failvzuy.png

What have I done wrong and more important, how can I fix it?
I found this post which describes the usual way, but I don't get how I can override my views "layoutSubviews" method, as the view is just a property of my UIViewController..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

‖放下 2024-10-19 01:03:52

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

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

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

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

因为您将所有视图中的 UI 元素作为 ivar,您无法再从 UIViewController 访问它们,对吗?因此,请确保您拥有正确的访问器/属性和方法来处理 UIViewController 中的视图(及其 ivars)。 ;)

alt text

祝你好运 ;)

编辑:2011/01/17

当您在控制器中并执行 [self.查看 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

If you want to create you own subclass of UIView and set it to UIViewController so you can implement layoutSubviews method, is very easy actually!.

UIViewController does not creates a view it expects the user to set the view but when using Interface BUilder there is a UIView already set for us.

To change the view do:

  • Create your subclass, here I will assume it is called MyView, and put there all your UI stuff (toolbars, buttons, etc).
  • Open your view controller file in IB (the *.xib)
  • Select the view (the view that we are changing)
  • Go to the Identity Inspector (or press Cmd+4)
  • Enter "MyView" in the class identity field

Since you put all your UI elements in the view as ivar you cannot access them from your UIViewController anymore right? So, make sure you have the correct accessors/properties and methods to handle your view (and its ivars) from UIViewController. ;)

alt text

Good Luck ;)

EDIT:2011/01/17

When you are in your controller and do [self.view xyz] you get the warning right?
That is because a UIViewController's view is declared to be an UIView object. And you subclassed UIView and set it in IB. So everytime you access the view you have to cast it or the compiler will think it is still the normal UIView which does not have the methods you have declared in MyView. Do like this:

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

This should work. But I don't understand why then got: "property 'view' type does not match super class 'UIViewController' property type" First try what I just said ;) Are you sure you Declared MyView as:

@interface MyView: UIView{
...
}
@property (...) XYZ *xyz;
...
@end
笑饮青盏花 2024-10-19 01:03:52

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

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

I added following code in the MyView class in the .m file and it helped me out of my misery:

- (void)awakeFromNib
{
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文