Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Last active December 14, 2015 02:19
Show Gist options
  • Select an option

  • Save lamprosg/5012520 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/5012520 to your computer and use it in GitHub Desktop.
(iOS) Going from Portrait to Landscape and back
//When the iPhone changes rotation, this method is triggered.
//Once you have 2 nibs on your controller with connected outlet collections (if necessary)
//and actions of both portrait and landscape nibs to the same method
//.
//You need to create to seperate outlets of both VIEWS in the viewcontroler .h file.
//@property (strong, nonatomic) IBOutlet UIView *portrait;
//@property (strong, nonatomic) IBOutlet UIView *landscape;
#define degreesToRadians(x) (M_PI * (x) / 180.0)
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
interfaceOrientation duration:(NSTimeInterval)duration
{
//Note: tabbar is 20 pixels so we have to make view bounds accordingly
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
self.view = self.portrait;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(0));
self.view.bounds = CGRectMake(0.0, 0.0, 320.0, 460.0);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
self.view = self.landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
self.view = self.landscape;
self.view.transform = CGAffineTransformIdentity;
self.view.transform =
CGAffineTransformMakeRotation(degreesToRadians(90));
self.view.bounds = CGRectMake(0.0, 0.0, 480.0, 300.0);
}
}
//When having the 2 versions of a button for example in outlet collections
//from the 2 portrait-lanscape views
//Here;s how to choose which button is clicked each time
- (IBAction)buttonTapped:(id)sender
{
if ([self.foos containsObject:sender])
{
for (UIButton *oneFoo in self.foos)
{
oneFoo.hidden = YES;
}
}
else
{
for (UIButton *oneBar in self.bars)
{
oneBar.hidden = YES;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment