I'm just trying to write a view based Application, which only uses the landscape Orientation.
Using this Code:
application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
in the applicationDidFinishedLaunching method the Application starts in landscape orientation.
The problem is, that if I create a landscape view in InterfaceBuilder, the view does not get shown up the way I created it... I need to create the view in portrait mode and imagine how it looks when it is in portrait mode (hope you understand what I mean?) - I insert already rotated images.
So, I got everything work now, but the InfoButton doesn't rotate (as the other buttons, but the others are already rotated images)...
Whats my fault? How to I tell the Application to use my View, which I designed in Landscape orientation?
Thanks so much.
regards
You may have to rotate your view (button) dependent on orientation:
UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
CGAffineTransform transform;
switch (orientation) {
case UIDeviceOrientationPortrait:
transform = CGAffineTransformIdentity;
break;
case UIDeviceOrientationPortraitUpsideDown:
transform = CGAffineTransformMakeRotation(180*M_PI/180);
break;
case UIDeviceOrientationLandscapeLeft:
transform = CGAffineTransformMakeRotation(90*M_PI/180);
break;
case UIDeviceOrientationLandscapeRight:
transform = CGAffineTransformMakeRotation(-90.0*M_PI/180);
break;
default:
transform = CGAffineTransformIdentity;
break;
}
[[myView layer] setAffineTransform:transform];
You don't need to create the view in portrait mode and imagine how it looks when it is in portrait mode. Just you need to create your view in your needed mode, then set your project setting to your needed mode. In Xcode on the left view you can see Project Navigator click on the blue icon of project name. Now on the middle view (editor view) you can see the summary tab, on this tab set your needed mode on the iPhone Development Orientations.
Related
I have one app always presenting in Portrait mode (in the summary of the Xcode project, only the portrait orientation is supported).
Now what I want to do is when I'm using the app, from any view controllers of the app, if I rotate the device in landscape right, the app presents a view controller (ARViewController.m) in landscape right, in fact the rotation to landscape right is the trigger to present ARViewController.m. But what I've experienced is, since the the first view controller only supports portrait and even if I orient the device in landscape right, the view controller (ARViewController.m) I want to present from the first one is in portrait too, not in landscape right.
Even if I write this in the second view controller (ARViewController.m), it doesn't autorotate (this view controller can be presented in every orientations):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return YES;
}
I have to rotate the iphone once after the second view controller (ARViewController.m) is presented to have all in order.
And here is how I call this second view controller (ARViewController.m) from the first view controller:
ARViewController *arVC = [[ARViewController alloc] initWithNibName:#"ARViewController" bundle:nil];
[self presentModalViewController:arVC animated:YES];
I'm calling it from "ViewController.m", this one is defined as the rootViewController in the AppDelegate.m.
This is the first time I'm doing such things, I've looked for solutions but still the same problem. Any tips on this?
I finally solved this problem, I suppose there are alternatives but this one works fine:
In fact I kept only Portrait in the orientation restrictions. Then when I turn the phone in landscape right or left, I call my ARViewController modally, but before loading it I force this view controller to landscape (in viewWillAppear) by making an appropriate rotation like here:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self transformView2ToLandscape];}
-(void) transformView2ToLandscape {
NSInteger rotationDirection;
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
if(currentOrientation == UIDeviceOrientationLandscapeLeft){
rotationDirection = 1;
}else {
rotationDirection = -1;
}
CGAffineTransform transform = [arController.viewController.view transform];
transform = CGAffineTransformRotate(transform, degreesToRadians(rotationDirection * 90));
[arController.viewController.view setTransform: transform];}
Edit: In Swift 4
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
transformViewToLansdcape()
}
func transformViewToLansdcape(){
var rotationDir : Int
if(UIDeviceOrientationIsLandscape(UIDevice.current.orientation)){
rotationDir = 1
}else{
rotationDir = -1
}
var transform = self.view.transform
//90 for landscapeLeft and 270 for landscapeRight
transform = transform.rotated(by: (rotationDir*270).degreesToRadians)
self.view.transform = transform
}
extension BinaryInteger {
var degreesToRadians: CGFloat {
return CGFloat(Int(self)) * .pi / 180
}
}
What i've found is that first of all, what's stopping the entire application from rotating is the project summary sheet. What you should do is deselect the restrictions in the project summary sheet & just put a method in each UIViewController like you'd like it to be. In the UIViewController that you have, make the method available for the landscape orientations & implement the method
[UIViewControllerSubclass willRotateToInterfaceOrientation:<interfaceOrientation> duration:<NSTimeInterval>]
what Apple says:
willRotateToInterfaceOrientation:duration:
Sent to the view controller just before the user interface begins rotating.
(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Parameters
toInterfaceOrientation
The new orientation for the user interface. The possible values are described in
UIInterfaceOrientation.
duration
The duration of the pending rotation, measured in seconds.
Discussion
Subclasses may override this method to perform additional actions immediately prior to the rotation. For example, you might use this method to disable view interactions, stop media playback, or temporarily turn off expensive drawing or live updates. You might also use it to swap the current view for one that reflects the new interface orientation. When this method is called, the interfaceOrientation property still contains the view’s original orientation.
This method is called regardless of whether your code performs one-step or two-step rotations.
so, this seems like the method you are looking for. Just implement this, and put your view calling code inside & it should work. (also, put one of these in the presented view to return when rotated back)
also, you should consider not presenting the view controller modally as it is rotating anyways and is obvious that the display is changing.
I want to support full device orientation in my iPad app which uses cocos2d.
To accomplish this I thought I'll return YES for shouldAutorotateToInterfaceOrientation like before when I was working on apps without cocos2d, but in this case my view doesn't resize properly.
To show you with what I ended up, it's example picture.
you can see that orientation is landscape but the view is still portrait.
Can anybody tell me what am I doing wrong?
Add the following function and perform respective changes
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
switch (toInterfaceOrientation) {
case UIInterfaceOrientationPortrait:
break;
case UIInterfaceOrientationLandscapeLeft:
break;
case UIInterfaceOrientationLandscapeRight:
break;
case UIInterfaceOrientationPortraitUpsideDown:
break;
default:
break;
}
}
You have to set the autosize property of your view. the quick way is to set it in the "size inspector"
I had asked this question once before but has had 0 attention and the person helping me has not responded in weeks, so please forgive me but I still do need help.
Im working with CGAffineTransformMakeRotation to flip a UILabel 180 degrees, I want the rotation based on UIOrientationPortrait and UIOrientationPortraitUpsideDown. I get 1/2 the result: when users flip to upside down (from portrait) the label transforms 180 and turns upside down as well (still facing the home button [Important])
BUT
when I rotate it back to Portrait the label stays in the flipped rotation state and does not stay with the home button. Thats what I need help with....
Here is the code i have:
#define degreesToRadian(x) (M_PI * (x) / 180.0)
...
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
TranslateLabel.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
}
You are rotating it upside down no matter what the orientation is. If you want it to rotate differently depending on the orientation, you have to do something like this:
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
switch(toInterfaceOrientation){
case UIInterfaceOrientationPortrait:
TranslateLabel.transform = CGAffineTransformIdentity;
break;
case UIInterfaceOrientationPortraitUpsideDown:
TranslateLabel.transform = CGAffineTransformMakeRotation(degreesToRadian(180));
break;
}
}
That way, it will rotate the label upside down when the device is upside down and it will return it back to normal when the device is on portrait orientation.
I think you want to switch on toInterfaceOrientation, setting the transform as you have above if the orientation is UIInterfaceOrientationPortraitUpsideDown and to CGAffineTransformIdentity when it's UIInterfaceOrientationPortrait. (This assumes you don't allow rotations to the other orientations.)
Ended up with doing this:
Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape? Works superb!
I have made an image in Photoshop which I want to use as background for my info-screen in my iPad application. The image contains text and some icons also. Around the image I have a border which is green.
The effect I am trying to achieve is:
When the user goes from the portrait orientation to landscape orientation I want the image (just the frame and the icons) to rotate 90 degrees so the image appear in landscape mode, instead of having a portrait view of the frame in landscape. The text and icons are decoupled (different layers which I have organized in different UIImageView's)they shall rotate 90 degrees.
What I have done already is the following:
Experimented a bit with this method:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:
and tried to do this:
self.btnFacebook.transform = CGAffineTransformMakeRotation(1.5707964);
which I think will rotate the btnFacebook property 90 degrees to the right (correct me if I am wrong) since it specifies positive radians. I can't seem to get it to work correctly though. Should'nt this rotate the button 90 degrees around its center coordinate in the frame? That wouldn't cause a change in the position of the button (the button is square)?
EDIT
Made an image:
As the image shows the image background (some custom graphics on it which looks good in both orientations) goes from portrait to landscape and rotates so it does not appear as portrait in landscape, the icons are decoupled from the background so they need to rotate as well because they need to be in the right orientation (it is social icons). The text however are on the same position, it only rotates 90 degrees without repositioning.
I understand your question as that you are trying to not to rotate the interface as whole but to rotate the purple and red squares individually.
I created a UIViewController that resembles your layout.
The black square is a UIView and the white square is there only so that I can tell when the black view rotates. This view is wired to view1 property on the controller.
There are 4 buttons that are wired to btnx (x runs 1 through 4) properties.
Since I no longer want to auto rotate the interface I only support the portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
To do the rotation manually, I added a method to the ViewController. It determines the angle it needs to rotate the components from portrait to current orientation, creates a rotation transform and applies it to all outlets.
- (void)deviceDidRotate:(NSNotification *)notification
{
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
double rotation = 0;
UIInterfaceOrientation statusBarOrientation;
switch (currentOrientation) {
case UIDeviceOrientationFaceDown:
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationUnknown:
return;
case UIDeviceOrientationPortrait:
rotation = 0;
statusBarOrientation = UIInterfaceOrientationPortrait;
break;
case UIDeviceOrientationPortraitUpsideDown:
rotation = -M_PI;
statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
break;
case UIDeviceOrientationLandscapeLeft:
rotation = M_PI_2;
statusBarOrientation = UIInterfaceOrientationLandscapeRight;
break;
case UIDeviceOrientationLandscapeRight:
rotation = -M_PI_2;
statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
break;
}
CGAffineTransform transform = CGAffineTransformMakeRotation(rotation);
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
[self.btn1 setTransform:transform];
[self.btn2 setTransform:transform];
[self.btn3 setTransform:transform];
[self.btn4 setTransform:transform];
[self.view1 setTransform:transform];
[[UIApplication sharedApplication] setStatusBarOrientation:statusBarOrientation];
} completion:nil];
}
The last thing to do is to get the OS to call my method. To achieve that I added the following code to application:didFinishLaunchingWithOptions: in the AppDelegate.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self.viewController selector:#selector(deviceDidRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
I am not sure whether this is exactly what you wanted but I believe it is at least similar so you can get some ideas from it how to solve your problem. I can provide source code for a working iPad application that I created to illustrate this.
I'm trying to create an iPhone application that is always in landscape mode, using the Utility application template. Here's what I did:
Create a new iPhone application project, using the Utility Application template
In Interface Builder, rotate all the views 90 degrees.
In Interface Builder, add a label to the middle of the MainView. Stretch it all the way across the view, set the alignment to centered, and set the autosizing springs so that it can stretch horizontally.
In Info.plist, add the key "UIInterfaceOrientation" with value "UIInterfaceOrientationLandscapeRight"
In the controller classes, change the shouldAutorotateToInterfaceOrientation methods to "return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);"
Run the app.
When I launch my app, it comes up in landscape orientation, but the main view only covers the top half of the display, and it is stretched horizontally. I get the same results in both the simulator and on an actual device. I've seen it with versions 2.2 and 2.2.1 of the SDK.
I have been able to work around the problem by adding the following step to the above:
Add "self.view.autoresizesSubviews = NO;" to RootViewController's viewDidLoad method after "[super viewDidLoad];".
If I do this, then it works as expected. But this feels like a hack. Why should this be necessary?
I don't think it is a transformation issue. All elements are drawn in the proper orientation and with the proper scaling. The problem seems to be that the bounds rectangles of the main view gets funky. It looks like the height of the main view is being cut by a little more than half, and the width is being increased by about 50%.
If I do the exact same set of steps using the View-based Application template instead of Utility, then everything works as expected. So I'm pretty sure the problem is specific to how a Utility application manages its views.
Anybody understand what's going on here?
I was going to say that setting this key does not rotate your interface; you still need to lay out your content in landscape mode and do the appropriate rotation using CFAffineTransform - see "Launching in Landscape Mode" in iPhone OS Programming Guide. Going to find the reference for you, I found this comment: "To launch a view controller–based application in landscape mode in versions of iPhone OS prior to v2.1, you need to apply a 90 degree rotation to the transform of the application’s root view in addition to all the preceding steps. Prior to iPhone OS 2.1, view controllers did not automatically rotate their views based on the value of the UIInterfaceOrientation key. This step is not necessary in iPhone OS 2.1 and later, however."
So if you're running pre-2.1, you need to add this code to your viewDidLoad method in your view controller. (Otherwise, can you post some code?)
-(void)viewDidLoad
// After loading the view, transform the view so that the co-ordinates are right for landscape
// As described in iPhone Application Programming Guide
// Weird, I'm sure this used to be needed, but it doesn't now. The one in CardScrollViewController is needed though.
{
[super viewDidLoad];
CGAffineTransform transform = self.view.transform;
CGPoint center = CGPointMake(kScreenHeight / 2.0, kScreenWidth / 2.0);
// Set the center point of the view to the center point of the window's content area.
self.view.center = center;
// Rotate the view 90 degrees around its new center point.
transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
self.view.transform = transform;
}
Jane describes the setting of UIInterfaceOrientation to UIInterfaceOrientationLandscapeRight (or UIInterfaceOrientationLandscapeLeft), and the rotation settings recommended in the documentation, but I used a slightly different block of code (to the same end) in my root view controller:
- (void)loadView
{
UIView *primaryView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
primaryView.backgroundColor = [UIColor clearColor];
// Start in landscape orientation, and stay that way
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight)
{
CGAffineTransform transform = primaryView.transform;
// Use the status bar frame to determine the center point of the window's content area.
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x);
CGPoint center = CGPointMake(60.0, bounds.size.height / 2.0);
// Set the center point of the view to the center point of the window's content area.
primaryView.center = center;
// Rotate the view 90 degrees around its new center point.
transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
primaryView.transform = transform;
}
self.view = primaryView;
[primaryView release];
}
In addition to that, I implemented the following delegate method in my root view controller:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ( (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}
Finally, I was encountering weird glitches with the Simulator not auto-rotating properly, so I needed to implement the following delegate method in my UIApplicationDelegate:
- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration;
{
// This prevents the view from autorotating to portrait in the simulator
if ((newStatusBarOrientation == UIInterfaceOrientationPortrait) || (newStatusBarOrientation == UIInterfaceOrientationPortraitUpsideDown))
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
}
After all that, my application was able to start in landscape (right) and stay in that orientation under the 2.0 firmware and in the Simulator.
Try setting the orientation property of the view to Landscape in the nib. This property can be found in 4th tab[Attributes Inspector] of Info View of the UIView under Simulated Metrices.