Reputation: 3617
I am adding UIInterfaceOrientation changes to the app. My app is not a tab-based or navigation based. I am adding subviews on top of other.
I went through the document why interface orientation changes not work? http://developer.apple.com/library/ios/#qa/qa1688/_index.html
I found that the main window controls the interface orientation changes.
But now as my app is not tab-based or navigation-based, is there still any way to achieve it?
P.S. : In my app only launch screens are working properly for interface orientation changes and not the added subviews.
Please help
Thanks in advance.
Upvotes: 0
Views: 2312
Reputation: 3013
Reading the comments posted above and the problem that you are facing what I can say is that You have added a hierarchy of subviews. Consider the following Case
1) I add a view controller (VC) , Navigation Controller , Tab bar controller directly to the window.. the subsequent views will get notify about any orientation changes properly.
2) I add a Parent view Controller to the window and later add views of some other view controllers to my parent view controller, in this particular case you will get the orientation change delegate methods being called properly in the Parent VC but not in the subsequent view controllers whose view you have added to your Parent VC
I once added a Base view controller to my application window where base controller had a ScrollView , I later added subviews of my four view controllers to scrollview.
(void)viewDidLoad {
DNAppDelegate *appDelegate = (DNAppDelegate*)[UIApplication sharedApplication].delegate;
CGRect viewCustormFrame = appDelegate.viewCustomTabBar.frame;
viewCustormFrame.origin.y = self.view.frame.size.height-35;
if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) {
viewCustormFrame.size.width = self.view.frame.size.width;
}
else {
viewCustormFrame.size.width = self.view.frame.size.width;
}
[self.view addSubview:appDelegate.viewCustomTabBar];
appDelegate.viewCustomTabBar.frame = viewCustormFrame;
[srclViewMainContent addSubview:appDelegate.homeController.view];
[srclViewMainContent addSubview:appDelegate.newsListController.view];
[srclViewMainContent addSubview:appDelegate.columnController.view];
[srclViewMainContent addSubview:appDelegate.whatsOnController.view];
currentVisisbleController = appDelegate.homeController;
[self resetAllSizes];
[super viewDidLoad];
}
In above case though i got all orientation delegate methods being called in base VC but not in the other Four VC.. so i had to tweak something like
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
DNAppDelegate *appDelegate = (DNAppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate.homeController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
[appDelegate.newsListController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
[appDelegate.columnController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
[appDelegate.whatsOnController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
DNAppDelegate *appDelegate = (DNAppDelegate*)[UIApplication sharedApplication].delegate;
currentPageOffset = srclViewMainContent.contentOffset.x/srclViewMainContent.frame.size.width;
[appDelegate.homeController willRotateToInterfaceOrientation:toInterfaceOrientation duration:(NSTimeInterval)duration];
[appDelegate.newsListController willRotateToInterfaceOrientation:toInterfaceOrientation duration:(NSTimeInterval)duration];
[appDelegate.columnController willRotateToInterfaceOrientation:toInterfaceOrientation duration:(NSTimeInterval)duration];
[appDelegate.whatsOnController willRotateToInterfaceOrientation:toInterfaceOrientation duration:(NSTimeInterval)duration];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
DNAppDelegate *appDelegate = (DNAppDelegate*)[UIApplication sharedApplication].delegate;
[self resetAllSizes];
CGRect visibleRect = CGRectMake(currentPageOffset*srclViewMainContent.frame.size.width, 0, srclViewMainContent.frame.size.width, srclViewMainContent.frame.size.height);
[srclViewMainContent scrollRectToVisible:visibleRect animated:NO];
[appDelegate.homeController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[appDelegate.newsListController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[appDelegate.columnController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[appDelegate.whatsOnController didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
i.e call these delegate methods on respective VCs from the Base VC. I had to do this because my application was completely build and later I had to change the architecture to allow scrolling among various views, But I had already implemented these views by taking four View controllers.
In a nutshell any VC (also Navigation Controller, tab Bar controller, split View controller)added directly to window will propagate these Orientation change delegate methods to subsequent VCs but a View controller will not pass these delegate methods to VCs coming down the lane
I hope this gives you some insight. Just in case if you come across any good solution do post in as an answer and share with us all.
Upvotes: 1
Reputation: 7461
Use following method to resize subview. OR use autoresizingMask
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation == UIInterfaceOrientationPortrait){
viewInformationContainer.frame = CGRectMake(0, 61, 320, 403);// here you can change resize or subviews
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
viewInformationContainer.frame = CGRectMake(0, 0, 480, 227);//here you can change resize or subviews
}
}
Hope, this will help you...
Upvotes: 1