views:

133

answers:

3

Hi there...

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.

A: 

Hello !

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 !

Vinzius
hey. thanks for your help - have edited my op because i still can't seem to get it to work. :( any ideas what to do now?
Thomas Clayson
+1  A: 

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;
}
VdesmedT
how do i rotate all the content?
Thomas Clayson
You don't :-) Since the status bar will always be up, the content is resized and does not really "rotate".
VdesmedT
hi... thanks for all your help - I still can't seem to get it to work. :( I'm not sure I'm understanding you properly - I have edited my OP to include what I've done given my understanding of your answer. Sorry if this is tedious.
Thomas Clayson
thanks for your help - after some more research i found out it was because i was using a UITabBarController - which won't auto rotate unless ALL the root view controllers autorotate. :p thanks :)
Thomas Clayson
Yep ! iTunesU videos of iPhone programming course at Stanford are great !!!
VdesmedT
A: 

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?

Raj
`[self.navigationController pushViewController:thePageIWantToAutoRotate animated:YES];` will try those methods, thank you.
Thomas Clayson
If you are doing it this way, then the contents SHOULD rotate, maybe they appear as clipped, but the view controller should rotate.
Raj