Reputation: 9830
I'm trying to change the position of UIViews
when the iPhone's orientation is changed. It's similar to this question; but instead of 3 views
, I'll be having 2 views
. When the iPhone is in portrait mode, I want the views
to be one on top of the other. When in landscape mode, they should be side by side.
So I tried the following method:
willAnimateRotationToInterfaceOrientation:duration:
But that method is deprecated. What can I do to check when the iPhone's orientation changed?
Update
I tried the following: Thanks to @Sean for the guidance!!
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.tableView.frame = CGRectMake(0, 0, 100, 200); // This Works
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
CGFloat viewWidth = self.view.frame.size.width;
CGFloat viewHeight = self.view.frame.size.height;
if (size.width >= size.height)
{
self.containerView.frame = CGRectMake(0, 0, viewWidth, 272);
self.tableView.frame = CGRectMake(self.containerView.frame.size.height, 0, viewWidth, viewHeight - self.containerView.frame.size.height);
NSLog(@"Portrait Mode"); // Get the NSLog, but frames don't change
}
else
{
self.containerView.frame = CGRectMake(0, 0, viewWidth / 2, viewHeight);
self.tableView.frame = CGRectMake(viewWidth / 2, 0, viewHeight / 2, viewHeight);
NSLog(@"Landscape Mode"); // Get the NSLog, but frames don't change
}
}
So when I change the frames in viewDidLayoutSubviews
, it works. But when I try changing them in viewWillTransition...
it doesn't change, but I get the NSLogs
.
Upvotes: 2
Views: 1919
Reputation: 2116
Apple recommends using the following method:
- (void)viewWillTransitionToSize:(CGSize)size
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
Example:
- (void)viewWillTransitionToSize:(CGSize)size
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
if (size.width >= size.height) {
// Landscape frames
self.subviewA.frame = CGRectMake(x, y, width, height);
self.subviewB.frame = CGRectMake(x, y, width, height);
} else {
// Portrait frames
self.subviewA.frame = CGRectMake(x, y, width, height);
self.subviewB.frame = CGRectMake(x, y, width, height);
}
}
Make sure you call the super class method as shown above when implementing this solution.
Upvotes: 2
Reputation: 11201
You can check the device orientation by using the following code:
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == 0) //Default orientation
//UI is in Default (Portrait) -- this is really a just a failsafe.
else if(orientation == UIInterfaceOrientationPortrait)
//Do something if the orientation is in Portrait
else if(orientation == UIInterfaceOrientationLandscapeLeft)
// Do something if Left
else if(orientation == UIInterfaceOrientationLandscapeRight)
//Do something if right
You can create two separate views according to your need and present them based on the device orientation.
Upvotes: 0