设备旋转 iPhone

发布于 2024-09-24 18:35:16 字数 685 浏览 2 评论 0原文

我所有的宽度、高度、位置和所有内容都是相对于视图等设置的,因此如果 iPhone 旋转,它应该像以前一样“扩展以适应”。 :)

我该如何告诉 iPhone 处于“横向模式”,即:刷新,但现在所有宽度都是高度,所有高度都是宽度?

谢谢 汤姆

编辑 这是我目前拥有的代码...它仍然不起作用 - 我似乎无法弄清楚如何使其工作

在我的视图控制器(不是根视图控制器)中我有这个方法:(

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

我也有尝试将此方法放入我的 root 视图控制器中,但这也不起作用)

然后在我的 viewDidLoad 方法中我有这个:

[self.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self.view setAutoresizesSubviews:YES];

这不起作用。 :( 至少在模拟器中不是。

All of my widths, heights, positions and everything are set relative to views and such, so if the iphone is rotated it should "expand to fit" as it were. :)

How would I go about telling the iphone to be in "landscape mode" ie: refresh, but now all widths are heights and all heights are widths?

Thanks
Tom

EDIT
Here is the code I currently have... it still doesn't work - I can't seem to figure out how to make it work

In my view controller (not the root view controller) I have this method:

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

(i have also tried putting this method into my root view controller, but that didn't work either)

Then in my viewDidLoad method I have this:

[self.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self.view setAutoresizesSubviews:YES];

This doesn't work. :( at least not in the simulator.

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

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

发布评论

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

评论(3

吃不饱 2024-10-01 18:35:16

根据您的期望设置视图的 AutoresizingMask 。

或实现 layoutSubViews 方法。

另外,不要忘记实现 viewController 的 shouldAutorotateToInterfaceOrientation 方法。流程如下。

  • 设备旋转
  • 根 ViewController 接收 shouldAutoRotateToInterfaceOrientation 事件
  • 如果 viewController 响应 YES,则 willRotateToInterfaceOrientation 事件将发送到控制器,
  • 控制器根据其 autoresizingMask 调整其视图大小code>
  • 如果视图的 autoresizesSubviews 属性为 true,则深入到子视图的过程
  • didRotateToInterfaceOrientation 事件将发送到控制器。

这是一个示例 viewController 代码

- (void)loadView {
    UIView* theView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    theView.backgroundColor = [UIColor blueColor];

    UIView* redRectangle = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 100.0f, 100.0f)];
    redRectangle.tag = 1;
    redRectangle.backgroundColor = [UIColor redColor];
    [theView addSubview:redRectangle];
    [redRectangle release];

    UIView* greenRectangle = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 120.0f, 100.0f, 100.0f)];
    greenRectangle.tag = 2;
    greenRectangle.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    greenRectangle.backgroundColor = [UIColor greenColor];
    [theView addSubview:greenRectangle];
    [greenRectangle release];

    UIView* yellowRectangle = [[UIView alloc] initWithFrame:CGRectMake(theView.bounds.size.width-110.0f, theView.bounds.size.height-110.0f, 100.0f, 100.0f)];
    yellowRectangle.tag = 3;
    yellowRectangle.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
    yellowRectangle.backgroundColor = [UIColor yellowColor];
    [theView addSubview:yellowRectangle];
    [yellowRectangle release];

    self.view = theView;
    [theView release];
}

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

Set the AutoresizingMask of your view according to what you expect.

or implement the layoutSubViews method.

Also, don't forget to implement the shouldAutorotateToInterfaceOrientation method of the viewControllers. The flow is as follow.

  • Device rotate
  • root ViewController receive the shouldAutoRotateToInterfaceOrientation event
  • If the viewController respond YES, the willRotateToInterfaceOrientation event is send to the controllers
  • the controller resizes its view according to its autoresizingMask
  • if the view's autoresizesSubviews property is true, the process drill down to the subviews
  • didRotateToInterfaceOrientation event is sent to the controller.

Here is a sample viewController code

- (void)loadView {
    UIView* theView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    theView.backgroundColor = [UIColor blueColor];

    UIView* redRectangle = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 100.0f, 100.0f)];
    redRectangle.tag = 1;
    redRectangle.backgroundColor = [UIColor redColor];
    [theView addSubview:redRectangle];
    [redRectangle release];

    UIView* greenRectangle = [[UIView alloc] initWithFrame:CGRectMake(10.0f, 120.0f, 100.0f, 100.0f)];
    greenRectangle.tag = 2;
    greenRectangle.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    greenRectangle.backgroundColor = [UIColor greenColor];
    [theView addSubview:greenRectangle];
    [greenRectangle release];

    UIView* yellowRectangle = [[UIView alloc] initWithFrame:CGRectMake(theView.bounds.size.width-110.0f, theView.bounds.size.height-110.0f, 100.0f, 100.0f)];
    yellowRectangle.tag = 3;
    yellowRectangle.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
    yellowRectangle.backgroundColor = [UIColor yellowColor];
    [theView addSubview:yellowRectangle];
    [yellowRectangle release];

    self.view = theView;
    [theView release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
生来就爱笑 2024-10-01 18:35:16

您可以通过设置自动调整视图的大小:

[ tmpView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];

因此,当您旋转时,它会自行调整大小。

但我认为它不会满足你的需要。
因此,也许您应该添加一个在设备旋转时调用的函数(在控制器中):

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    [ self refreshLayouts ];

}

然后在刷新布局中您可以根据需要更改自己的宽度/高度。

祝你好运 !

You can adjust the size of your view automatically by setting :

[ tmpView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];

So when you rotate it will resize itself.

But I think it won't do what you need.
So maybe you should add a function (in controller) which is called when device rotate :

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    [ self refreshLayouts ];

}

Then in refreshLayouts you change yourself the width/height like you want.

Good Luck !

吻泪 2024-10-01 18:35:16

首先检查视图控制器的旋转事件回调方法是否被调用:

-willRotateToInterfaceOrientation:duration:
-didRotateFromInterfaceOrientation:

如果这些方法没有被调用,那么就没有必要继续下去,因为你的 viewController 根本没有旋转!

这让我感兴趣 - “在我的视图控制器中(不是根视图控制器)”。如何将视图控制器添加到窗口层次结构中?您是要呈现它,还是只是将其视图作为子视图添加到根视图控制器?

First check if your view controller's call back methods for rotation events are being called or not:

-willRotateToInterfaceOrientation:duration:
-didRotateFromInterfaceOrientation:

If these are not called, then there is no point in going further because your viewController is not rotated at all!

This interests me - "In my view controller (not the root view controller)". How are you adding your view controller to the window hierarchy? Are you presenting it, or just adding its view as a sub-view to, say a root view controller?

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