Landscape only View in Cocoa Touch - objective-c

Is there to way to fix a view's orientation in a landscape position, regardless of the device's current position?
To be more specific, I have a UIWebView in a UINavigationController that should always show its content as if the device were rotated 90 degress counterclockwise. I only want the web view to be restricted to this orientation; every other view in the app should behave normally.

Yes. In your info.plist file, define:
UIInterfaceOrientation UIInterfaceOrientationLandscapeRight
EDIT:
The above changes the orientation for the whole app, and you say you don't want that. Can the user move the phone to achieve the orientation you need?
If you don't want to rely on the user to change the phone to that orientation, try:
self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);

In the controller for the view, implement shouldAutorotateToInterfaceOrientation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
return UIInterfaceOrientationIsLandscape(orientation);
}
Or if you only want to allow Left Landscape (the default for the Youtube app, for example)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
return orientation == UIInterfaceOrientationLandscapeRight;
}

Related

How to make screen scrollable when orientation is landscape xcode

Hi all I'm developing an app in xcode for iPhone. i've many label in that it looks good when orientation is Portrait but when it is landscape all labels will overlap on each other, so i want to make it as scrollable screen only when it is in landscape mode. please help me Thanks in Advance.
Add UIScrollView in self.view.
Now set frame of yourScrollView according to orientation and its content size according to it. But in protrait frame and content size will be equal so there will be no scrolling.
Now in landscape, frame and content size will be not be equal so there will be scrolling
Note : Adjust UIlements's fame according to orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
[yourScrollView setFrame:CGRectMake(0,0,320,460)];
[yourScrollView setContentSize:CGSizeMake(320,460)];
//change frame of UIELement(labels) here according to orientation
}
else
{
[yourScrollView setFrame:CGRectMake(0,0,480,300)];
[yourScrollView setContentSize:CGSizeMake(480,460)];
//change frame of UIELement(labels) here according to orientation
}
return YES;
}

Rotate one UIViewController in UITabBar application

I have uitabbar application and I Want to rotate just one ViewController with chart in landscape mode. It's possible to do that?
There is no easy way to have only one view in landscape mode, while the others are in landscape, nor an easy way to programmatically switch to landscape mode.
One possible approach would be using a CGAffineTransform to transform your view in your viewWillAppear (i.e., right before the view is shown):
- (void)viewWillAppear:(BOOL)animated; {
//-- Adjust the status bar
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
//-- Rotate the view
CGAffineTransform toLandscape = CGAffineTransformMakeRotation(degreesToRadian(90));
toLandscape = CGAffineTransformTranslate(toLandscape, +90.0, +90.0 );
[self.view setTransform:toLandscape];
}
Hope this works for you.
Only if it's modal, if it's part of the tabBarController, you need to support orientation change for all your controller

iPhone navigation bar rotation issue when coming back from background

Ok, so after weeks of headache, I've given up and decided to turn to expert resource online!
Basically, I am trying to get my iphone application to view in a forced landscape mode (to be precise, for a graph) for just 1 view controller in otherwise a portrait-orientation dominated navigation-bar application.
I got everything working the way it should, EXCEPT when I put the application into background and return, the application returns with the navigation bar moved to where it usually sits in Portrait orientation mode but rotated 90', whilst every others like status bar, the main view are all still in landscape mode.
I've tried to manually correct the navigation bar orientation afterwards, but cocoa seems to ignore this bit of code. It's almost as if the self.view.transform doesn't work when the application returns from background mode. Any suggestions?
LandscapeViewController.m
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
-(void)viewWillAppear:(BOOL)animated {
self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
}
This image shows you can see what I mean
http://i.stack.imgur.com/Qx1Si.png
Have you tried this?
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
You should not always return NO from shouldAutorotateToInterfaceOrientation: but return YES for the orientation you support. The iOS then handles the rest.

Stop UIToolbar from Disappearing in Landscape Orientation - iPad

Not sure why this happens or how to stop it, my UIToolBar on the details viewcontroller is only visible during portrait view. I want it visible at all orientations. How do I do that? Thank you.
I encountered the same problem by just dragging a UIToolBar on to my view and docking it on the top of the window. It showed up in landscape but not portrait. Interface Builder - at least the one embedded in Xcode 4 - doesn't seem to do the right thing with the resize masks.
While Kshitiz's answer above will work, it has a couple of flaws. As coded, it does not support all four orientations. More importantly, it's not resolution independent.
A better solution is briefly described in enamrik's comment, so credit should go to him/her. Here are the steps:
Select the tool bar in Interface Builder.
Open the Size inspector.
In the Autosizing box, select the left, right and top "i-beams" on the exterior of the square. This keeps the position of the toolbar fixed relative to the sides of the view when the view is resized.
Inside the Autosizing square, select the horizontal line with arrows on both ends. This causes the size of the toolbar to change in sync with the parent view.
in your this function of view controller reset view frame bro
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Override to allow orientations other than the default portrait orientation.
if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
self.view.frame = CGRectMake(0, 0, 703,768);
} else {
self.view.frame = CGRectMake(0, 0, 768, 1024);
}
return YES;
}
and your tool bar frame too
good luck
Faced the same problem when I add UIPickerView programmatically and add UIToolBar for the PickerView. Just need to add [.flexibleWidth,.flexibleHeight] for the UIPickerView. eg:-
let statTypePicker = UIPickerView()
And then add
self.statTypePicker.autoresizingMask = [.flexibleWidth,.flexibleHeight]

Why aren't my views being positioned correctly on interfaceOrientation changes?

I have a UIViewController one UIWebView in it. I'd like the UIWebView to be positioned in the centre of the iPad screen in landscape and portrait modes. So, I've implemented it like this
// UIViewController
// InfoGraphicView is the UIWebView
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[self layoutPortrait];
} else {
[self layoutLandscape];
}
}
- (void)layoutLandscape {
NSLog(#"Layout Landscape");
infoGraphicView.frame = CGRectMake(100, 100, 936, 700);
}
- (void)layoutPortrait {
NSLog(#"Layout Portrait");
infoGraphicView.frame = CGRectMake(100, 100, 700, 936);
}
However, it's not behaving as I expected. In the above code, I would expectt he UIWebView to be 100px (or points or whatever the unit is) away from the top and the left. But it's not. In Portrait mode it appears flush with the top left of the screen, and in Landscape mode it seems to be partially offscreen in the top left.
If I set the frame as CGRectMake(-100, 100, 700, 936) then I get it positioned in the center of the screen as I'd like it to be, but I've no idea why.
As usual, there's most likely something simple I'm overlooking but I can't figure it out. Any help greatly appreciated as always.
The coordinates you set on infoGraphicView are relative to its superview, not to the screen generally. And views don't necessarily clip their subviews. Furthermore, the shape set automatically to self.view will depend on the scaling flags set in Interface Builder. However, I think that by default it is set to fill the whole screen.
That said, I think the mistake is in your use of willRotateToInterfaceOrientation:duration:. That is called before the rotation begins, so self.view has the old size (ie, it'll still be portrait sized if rotating from portrait to landscape and vice versa). Probably better to hook willAnimateRotationToInterfaceOrientation:duration: — then the correct size has been set and you'll be within the CoreAnimation block so your view will grow/shrink as part of the rotation animation.
It's also worth checking which resizing flags you have set on infoGraphicView. They'll take effect automatically, in addition to any changes you make. So you probably want to disable them all.
This probably is an issue with the view that the web view is in. The coordinate system used is that of the view’s superview. If that view isn’t being resized on rotation, then you’ll see unexpected layout like this. You can access the superview of a view through the superview property; one way to see its frame would be to use its description. Put this line in one of your layout methods:
NSLog(#"Superview: %#", [infoGraphicView superview]);
That should print out a description of the view.
Once you get that figured out, if you want the web view to have the same layout, you can use its autoresizingMask property. If you set it like this:
infoGraphicView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Then the view will automatically change its width and height to keep the top, left, right, and bottom margins the same.