I have the following code to display a UIViewController in horizontal orientation ontop of a UITableViewController.
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[self.view addSubview:landscapeChartViewController.view];
}
if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
if(landscapeChartViewController != nil)
[landscapeChartViewController.view removeFromSuperview];
}
}
When the phone rotates, the view doesn't take up the entire screen.
The user is also able to scroll, thus showing the rest of the UITableViewController. I don't want the UITableViewController to be there in horizontal orientation.
Try [self.view addSubview:landscapeChartViewController.view];
Assuming you've set your frames correctly, this should be what you're looking for.
landscapeChartViewController.view.frame = self.view.bounds, perhaps?
Also keep in mind that the orientation change can happen "in the background" if you're using a tab bar controller, so you might also want to check the current orientation in -viewWillAppear:.
Related
while in the simulator the problem don't subsist, i've a problem with a view after auto rotating to landscape orientation. When I load the app in portrait, all works fine, but when i rotate the device (iPod touch 1st gen. 3.1.3) to landscape the result is this: http://imageshack.us/photo/my-images/155/img0021n.png/ .
If i switch to the second view ("Riepilogo" i.e. a table view) and then back to the first, the view fit perfectly in landscape, but if I rotate again to the portrait orientation, the problem is the same as above.
Here is the code of shouldAutorotateToInterfaceOrientation:
- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation{
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
[self.scrollView setContentSize: CGSizeMake(480,416)];
} else {
[self.scrollView setContentSize: CGSizeMake(320, 416)];
}
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight); }
P.S. The view is an instance of UIScrollView
Resizing a scrollview doesn't resize its contents.
I am working on a project where I have added UIWebviews in the UIscrollview to show the description. I did this because I want to add the swipe effect to move to the new description page. Now, I want to resize that UIscrollview and its content (i.e uiwebview) when the orientation changes (i.e portrait to landscape or Viceversa).
Please give me any sample code or any suggestions for it.
To resize the webview you have to write:-
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
webview.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * someValue1,
scrollView.frame.size.height * someValue2);
}
You can put your code in the following code block :
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if ([self isPadPortrait]) {
// Your code for Portrait
// set frame here
} else if ([self isPadLandscape]) {
// Your code for Landscape
// set frame here
}
and following code will take care of orientation change :
- (BOOL)isPadPortrait
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
&& (self.interfaceOrientation == UIInterfaceOrientationPortrait
|| self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}
- (BOOL)isPadLandscape
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
&& (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight
|| self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft));
}
You can set the autoresizingMask of scrollView and add this code to your .m file according to your requirement.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * someValue1,
scrollView.frame.size.height * someValue2);
}
Here is a link to a similar question and their solution:
Scale image to fit screen on iPhone rotation
They used a combination of AutoresizingMasks and ContentModes for both the ScrollView and, in their case, an ImageView, although I imagine the same solution would work for your WebView.
when ever the orientation changed
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
// this delegate method will called if we set should auto orientation return yes;
when ever we changed orientation so set frames..
}
for example
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
appDelegate.interface=interfaceOrientation;
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
// SET FRAMES FOR LANDSCAPE HERE
}
if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationPortrait)
{
//SET FRAMES FOR Portrait HERE
}
}
I have a view controller in my iphone app setup so that when I change the orientation from portrait to landscape, I change the view. When I change the orientation back from landscape to portrait, the initial view comes back, except this time it is all crammed into the left hand side of the screen. Eventually, when I change orientations enough times everything disappears completely. Is this a common issue beginners have? What could I be doing wrong?
In my root controller I am allowing the orientation to change only when a specific view is being shown with this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (self.currentView == (NSInteger*)Chart || self.currentView == (NSInteger*)Detail) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
Where self.currentView is an enum of what view I currently have up. The Detail view I want to make sure stays as a portrait view, but when I change the orientation while on that view I want it to change the view to the Graph. Again, this works fine the first time, but when I change back from Graph to Detail, it crams all the controls on the Detail view to the left hand side of the screen.
Here is how I'm changing the view:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if (self.currentView == (NSInteger*)Detail && (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"changeView" object:self userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:Chart] forKey:#"view"]];
}
if (self.currentView == (NSInteger*)Chart && (toInterfaceOrientation == UIInterfaceOrientationPortrait)) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"changeView" object:self userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:Detail] forKey:#"view"]];
}
#justin I once did this which got me into same situation as you are. May be you can check if you haven't done something like this
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
CGRect rect;
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
rect = CGRectMake(tableView.frame.origin.x,tableView.frame.origin.y,
tableView.frame.size.width - 50, tableView.frame.size.height - 30);
}
else {
rect = CGRectMake(tableView.frame.origin.x,aBar.frame.origin.y,
tableView.frame.size.width, tableView.frame.size.height);
}
[tableView setFrame:rect];
return YES;
}
All I wanted was a table view with small frame in Portrait mode, without saving the original Frame I was trying to reduce its width and height which eventually brought the table view to a very small size after multiple rotation..
Lolzzz. I should have first saved the original tableview frame and then done something like this
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
tableView.frame = CGRectMake(tableView.frame.origin.x,tableView.frame.origin.y,
tableView.frame.size.width - 50, tableView.frame.size.height - 30);
}
else {
tableView.frame = originalTableViewFrame;
}
check if you have autoresizeSubviews ON (in XIB/Inteface Builder) on your view and possibly parent views and try to turn it off if you are changing frame manually , this solved it for my case
My landscape subview is being cut off by the previous view, why is it like that?
Cut off as in the previous view is at the bottom part around 1/3 quarter of the screen even when the new subview is added. Something like this > http://i41.photobucket.com/albums/e253/welzenn99/123.png
you need to set that view's frame in
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
OR in
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
in the method where you tell the subview to come out ([self.view addSubview:someView.view];) add
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if ((orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)) {
//rotate to landscape
someView.view.frame = CGRectMake(0,0,480,320);
}
else if (orientation == UIDeviceOrientationPortrait) {
//rotate to portrait
isomeView.view.frame = CGRectMake(0,0,320,480);
}
worked for me. good luck
My navigation controller's navigation bar won't change the height when rotated to landscape.
see it stays at 44 pixels instead of 34 i think.
What do i do to fix this?
You have to add your navigation controller directly as a subview to your window, otherwise this doesn't work automatically. (It is not necessary to change the frame of your navigation bar manually.)
The -[application:didFinishLaunchingWithOptions:] method of your AppDelegate should contain something like
[window addSubview:self.yourNavController.view];
To get an example where this works automatically, you can also create a new navigation-based app in XCode and add an implementation for the shouldAutorotateToInterfaceOrientation: method of the RootViewController that always returns YES.
During the autoRotation method of your class, change the frame of your navBar like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if((self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight))
{
self.navigationController.navigationBar.frame = CGRectMake(0,0,480,32);
}
else if((self.interfaceOrientation == UIInterfaceOrientationPortrait) || (self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))
{
self.navigationController.navigationBar.frame = CGRectMake(0,0,320,44);
}
else
{
assert(false);
}
}