iOS willRotateToInterfaceOrientation proper usage - iphone

I have a very simply UIViewController, and I'm trying to figure out how to use willRotateToInterfaceOrientation. my UIViewController has a very simple viewDidLoad method:
-(void)viewDidLoad {
[super viewDidLoad];
theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
theBar.tintColor = [UIColor orangeColor];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"The Title"];
item.hidesBackButton = YES;
[theBar pushNavigationItem:item animated:YES];
[item release];
[self.view addSubview:theBar];
}
So basically, I just have a UINavigationBar at the top of my controller. That's it. I implemented some methods for rotation, based on what I found online:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) {
theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 640, 48)];
}
}
So, I launch the app in portrait mode, and then I twist in in landscape mode. And basically, theBar still stays it's normal size, and doesn't get resized. I'm sure this is a silly question, but what is the proper way to use the rotation capability? I want to make it so that it also works if the app is launched in landscape mode. What is the best way to initialize my components when the UIViewController first launches, keeping in mind that I want support for both orientations, and also keeping in mind that I want to be able to change the size of everything based on orientation changes throughout the duration of the life of the UIViewController? Thanks!

What you want to do is change the frame of your existing theBar object, and not instantiate a new one. You can do that with something like this:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation
duration:(NSTimeInterval)duration {
CGRect f = CGRectMake(0,
0,
CGRectGetWidth(self.view.frame),
CGRectGetHeight(theBar.frame);
theBar.frame = f;
}
Note that the value of self.view.frame is used, which contains values post rotation. Also note that the function I'm using here is different than yours. I haven't tested it with the function you're using, so I can't say if that'll work or not. Finally, you can avoid this altogether by just setting the autoresizingmask on theBar in viewDidLoad instead:
[theBar setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];

Related

GameCenter Orientation Problems

I got a lot of problems implementing Game Center.
My game its based on Cocos2d But my Menu I do it with XIB. So the main window i make it like this:
UIWindow
UINavigationController
Navigation Bar
UIViewController [Menu]
UINavigationItem
When i click on Play I create a new View Controller called Menu2Game.h.m.xib And i add the OpenGL to this view, and then Menu2Game add it to my menu View
menu2Game = [[Menu2Game alloc] initWithNibName:#"Menu2Game" bundle:nil];
[menu2Game setView:glView];
[self.view addSubview: menu2Game.view];
I don't know why I can't add it directly... But when I click on leaderboards I do the same thing. Presenting the view directly on Menu doesn't work.
UIViewController *tempView = [[UIViewController alloc] init];
[self.view addSubview:tempView.view];
[[GameCenter sharedGameCenter] showLeaderboard:tempView];
And when I call my leaderbords its this code.
if ([self isGameCenterAvailable]) {
GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];
if (leaderboardController != nil) {
screen = screen2;
leaderboardController.leaderboardDelegate = self;
[screen presentModalViewController: leaderboardController animated: YES];
[leaderboardController setCategory:#"015.1"];leaderboardController.view.frame.origin.y);
leaderboardController.view.bounds = CGRectMake(0, 0, 480, 320);
leaderboardController.view.center = CGPointMake(240, 160);
}
}
The problem here its that doesn't respects the application Orientation
If its in Landscape. It start in 80, 320, And finish in 480, 320
It appears and you can use it but appears not in ----> this direction it moves like with 45 degrees
And if you hold the phone in Portrait [Any]. It appears only a small fragment of the leader boards, Facing the real orientation, but the coordinated are different it takes the ones you are holding. And in my plist its not permuted portrait.
How could i fix this? I got 1 week to finish this game and this is the only thing I miss.
Thanks to everybody
I don't know why but just create should rotate and return true.., and eliminate all the code that modify the game center
this one:
[leaderboardController setCategory:#"015.1"];leaderboardController.view.frame.origin.y);
leaderboardController.view.bounds = CGRectMake(0, 0, 480, 320);
leaderboardController.view.center = CGPointMake(240, 160);
}

how to set the label postion from portrait to landscape in a view

i am doing application where i have taken two views Portrait and Landscape. i am creating a label programitically and calling it by parameter passing.As i want the position of the label to be changed in the landscapeView so i am taking two views.Instead i want to do in a SingleView
instead of using two views i want to have only one view and set the label position accordingly in portrait and landscape.
so please suggest me how to have only one view and change the textLabel position according to the view..please suggest me with sample code.
At present i am using the below code
[self.portraitView addSubview:[self createLabel:CGRectMake(450,140,60,20):#"Ratings:"]];
[self.landscapeView addSubview:[self createLabel:CGRectMake(600,140,60,20):#"Ratings:"]];
-(UILabel*)createLabel:(CGRect)frame :(NSString*)labelTitle {
UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.text = labelTitle;
}
Make your UILabel *myLabel a class variable by declaring it in your .h file.
Call the createLabel method in the viewDidLoad method of your viewController based on the correct device orientation.
-(void) viewDidLoad
{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation
if(orientation == UIDeviceOrientationPortrait)
CGRect frame = CGRectMake(450,140,60,20);
else
CGRect frame = CGRectMake(600,140,60,20);
myLabel = [[UILabel alloc] initWithFrame:frame];
[UIFont fontWithName:#"Arial" size:13];
myLabel.text = labelTitle;
}
Reset the frame in this method
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandScape)
{
mylabel.frame = CGRectMake(600,140,60,20); //your landscape frame
mylabel.text = #"Ratings:";
}
else
{
mylabel.frame = CGRectMake(450,140,60,20); // your portrait frame
mylabel.text = #"Ratings:";
}
}
You will need to dig around a little bit for covering all the possible orientations in this method.
Cheers!!!
in your code there is a method called
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIInterfaceOrientationLandScape)
//set label frame here
}
Implement the following two methods in your viewcontroller.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
and
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
In willRotateToInterfaceOrientation:duration: method, set the required frame to the label instance.
You should set the labels frame in the layoutSubviews method of the UILabel's superview and check the superviews bounds or the interface orientation to determine the appropriate frame for the label. If you also use autoresizingMask on the UILabel correctly, the transition will be animated nicely.
Also: why do you use two views? instead of applying different layouts to one?

How to mimic the resizable StatusBar in the Spotify iOS app

I've been struggling to figure out how Spotify creates the UI for when the app goes into offline mode. They make it seem like the StatusBar has resized, but in reality they're just putting a view below, and resizing all controllers throughout the app. I've tried subclassing UINavigationController, subclassing UIWindow, resizing the window, but nothing seems to work for every case.
The interesting thing about the Spotify app, is that their solution seems to still work when iOS' own UIViewController subclasses are presented modally (as seen in the image below, showing apple's MFMailComposeViewController - you can tell it's not a custom controller because of the UIBarButtonItems).
If anyone has any insight into how this is possible, that would be awesome.
it's a very dangerous thing to do. I've done in the past and I had nightmares. Below the code that works in iOS4 and supports orientation changes.
- (void) _adjustViewControllerforTicker {
TickerView* vv = [ApplicationContext getTickerView];
if ([PreferenceDataModel isFxTickerOn]&& self.navigationController.view.frame.origin.y==0) {
CGRect tableRect = self.tableView.frame;
self.tableView.frame = CGRectMake(tableRect.origin.x,tableRect.origin.y, tableRect.size.width, tableRect.size.height -20);
UINavigationController *nav = self.navigationController;
CGRect gframe = CGRectOffset(self.navigationController.view.frame, 0, 20);
self.navigationController.view.frame = gframe;
if (!vv) {
vv = [[TickerView alloc] initWithFrame:CGRectMake(0, 0, 480, 20)];
[nav.view addSubview:vv];
[vv release];
self.tableView.contentInset=UIEdgeInsetsMake(0,0,20.0,0.0);
[ApplicationContext setTickerView:vv];
}
if (![PreferenceDataModel isTickerOn]) {
self.tableView.contentInset= UIEdgeInsetsZero;
if (vv){
[vv removeFromSuperview];
vv=nil;
[ApplicationContext setTickerView:nil];
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self _adjustViewControllerforTicker];
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self _adjustViewControllerforTicker];
TickerView* vv = [ApplicationContext getTickerView];
if ([vv count]) {
[vv startAnimation];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self _adjustViewControllerforTicker];
}
And this is how it looks:

touchesBegan after didRotateFromInterfaceOrientation

I noticed that after an orientation change from portrait to landscape, I'm not getting touchesBegan events for some parts of my view any longer. I suppose that this is because I'm not informing my UIView about the dimension change of my window's frame after the device rotation.
I'm setting up everything programmatically (UIWindow, UIViewController, UIView) like this:
myViewController = [[myUIViewController alloc] init];
myWindow = [[UIWindow alloc] initWithFrame: rect];
myView = [[myUIView alloc] initWithFrame: [myWindow bounds]];
[myViewController setView:myView];
[myWindow addSubview:myView];
[myWindow setFrame:rect];
[myWindow makeKeyAndVisible];
When I get the didRotateFromInterfaceOrientation notification, I'm updating the window frame like this:
[[[self view] window] setFrame:rect];
But after that, my UIView does no longer get touchesXXX events for all areas. It seems that only the areas of the previous frame are still reporting events. So my question: Is there anything else I need to do in didRotateFromInterfaceOrientation to inform my UIView about the dimension change?
Thanks for help!
EDIT: Do I have to reposition the UIView on didRotateFromInterfaceOrientation() or is this done automatically? I noticed that the "transform" property of my UIView is set to a transformation matrix when the orientation changes. However, this makes it very hard to reposition my view. The docs say that the "frame" property can't be used when a transformation is active, so I tried to modify the "center" property to reposition my view, but this also doesn't work correctly. I want to move the view to the top-left corner, so I set "center" to (screenwidth/2,screenheight/2) but it doesn't position the view correctly :( Any idea or info what must be done to get the events right in orientation mode?
I have had this same problem, and I believe it is a frame issue.
I had to manually set the rects depending on orientation, as well as set the userInteractionEnabled on the main view, like:
if (appOrientation == 0)
appOrientation = [UIApplication sharedApplication].statusBarOrientation;
if (appOrientation == UIInterfaceOrientationLandscapeLeft || appOrientation == UIInterfaceOrientationLandscapeRight) {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
myView.frame = CGRectMake(0, 0, 1024, 768);
} else {
[[UIApplication sharedApplication] setStatusBarHidden:YES];
myView.frame = CGRectMake(0, 0, 768, 1024);
}
myView.userInteractionEnabled = YES;
OK I know this is an old question but this is how I fixed this issue.
In my situation I had a storyboard with a view that would be displayed either in portrait or forced to landscape mode depending on a user setting.
My app displays the statusBar at all times except for when I'm showing this view.
To make this all work, the transformations had to be applied in the viewWillAppear method for one. I had the following code in the viewDidAppear method at first and that messed with the bounds for the touchesBegan event I guess.
Here's the viewWillAppear code:
- (void)viewWillAppear:(BOOL)animated
{
// Hide the status bar
[[UIApplication sharedApplication] setStatusBarHidden:YES];
// This is to offset the frame so that the view will take the fullscreen once the status bar is hidden
self.navigationController.navigationBar.frame = CGRectOffset(self.navigationController.navigationBar.frame, 0.0, -20.0);
// If view is to be displayed in landscape mode
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"orientation"])
{
// Change status bar orientation
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft];
// Turn the view 90 degrees
[self.navigationController.view setTransform:CGAffineTransformMakeRotation(M_PI/2)];
// Set the frame and the bounds for the view
// Please Note: Frame size has to be reversed for some reason.
[self.navigationController.view setFrame: CGRectMake(0,0,320,480)];
[self.navigationController.view setBounds: CGRectMake(0,0,480,320)];
// Make sure user can interact with view
[self.navigationController.view setUserInteractionEnabled:YES];
}
}
Any other thing that had to happen layout wise, have to happen in the viewDidAppear method. For instance I had an image that covered the whole view and the frame had to be set depending on the orientation. Setting the frame in the viewWillAppear gave weird results but the same code worked perfectly in viewDidAppear.
Hope this helps someone as I banged my head for 6 hours on this thing.

My view is displaying y=-20 dispite its frame set to y=0. After rotation it snaps back to y=0

I started by creating a universal window based app. Starting with the iPhone version I created a UIViewController and associated nib.
My App delegate:
rootViewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
[window makeKeyAndVisible];
[window addSubview:rootViewController.view];
return YES;
My RootViewController:
- (void)viewDidLoad {
[super viewDidLoad];
adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero()];
[self.view addSubview:adBannerView];
}
I've tried instanciating buttons instead of the adBanner and I get the same result.
My RootViewController's nib has not been changed since x-code created it for me.
My MainWindow_iPhone.xib also is stock.
What's causing this?
Update
After changing the app's orientation the adBannerView (or button...) will snap into the correct place at y=0. I've tried setting adBannerView's y location to 20 presumably to compensate for the status bar and that makes everything display correctly until I change orientation. Then everything moves down 20 pixels and will leave a 20 pixel space between the adBannerView and the status bar.
Try to add the next line in your viewDidLoad (right after [super viewDidLoad];):
self.view.frame = [[UIScreen mainScreen] applicationFrame];
CGRectZero is literally a zero rect (0, 0, 0, 0), so ADBannerView should never show up if it really has a width and height of 0. You probably want to try initWithFrame:self.view.frame or so…
You should set the size identifier before adding the view:
- (void)viewDidLoad {
[super viewDidLoad];
adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero()];
if(UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
else
adBannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
[self.view addSubview:adBannerView];
// now you can treat it like any other subview
// For example, if you want to move it to the bottom of the view, do this:
CGRect frame = adBannerView.frame;
frame.origin.y = self.view.frame.size.height - frame.size.height;
[adBannerView setFrame:frame];
}
Whenever the interface rotates, you should notify the banner to change its size.
Assuming you have access to WWDC videos (which is available for free), check video session 305. It demos adding the banner.