设备旋转iphone [英] device rotation iphone

查看:154
本文介绍了设备旋转iphone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的所有宽度,高度,位置和一切都是相对于视图等设置的,所以如果iphone旋转,它应该扩大到适合。 :)



我如何告诉iphone处于横向模式即:刷新,但现在所有的宽度都是高度而所有的高度都是宽度? p>

感谢
Tom



EDIT
我目前有...它仍然不工作 - 我似乎不知道如何使它工作



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

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

(我也试过把这个方法放到我的< 然后在我的 viewDidLoad 方法中,我有这样的:

<

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

这不起作用。 c>



或实施 layoutSubViews 方法。



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




  • 装置旋转

  • root ViewController接收 shouldAutoRotateToInterfaceOrientation
  • 如果viewController响应YES,则会将 willRotateToInterfaceOrientation 事件发送到控制器

  • 根据其 autoresizingMask

  • 如果视图的 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;
}


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.

解决方案

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

这篇关于设备旋转iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆