I want to be able to change orientation only programmatically. Wrote function, which rotates screen, as I want.
UIInterfaceOrientation toIOSOrientation(ScreenOrientation desiredOrientaion) {
if (desiredOrientaion == ScreenOrientation::Landscape || desiredOrientaion == ScreenOrientation::LandscapeReversed) {
return UIInterfaceOrientation::UIInterfaceOrientationLandscapeLeft;
} else if (desiredOrientaion == ScreenOrientation::Portrait) {
return UIInterfaceOrientation::UIInterfaceOrientationPortrait;
} else {
return UIInterfaceOrientation::UIInterfaceOrientationPortrait;
}
}
void iOSTools::changeOrientation(ScreenOrientation desiredOrientaion) {
NSNumber* value = [NSNumber numberWithInt:toIOSOrientation(desiredOrientaion)];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
}
But I have to turn on my two orientation in application manifest:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
</array>
If I'm doing so, user is able to use both of this orientations. If I want to forbid this, I have to override this function, but how I should do it in Qt?
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
if (shouldRotate == YES){
return UIInterfaceOrientationMaskAll;
}else{
return UIInterfaceOrientationMaskPortrait;
}
}
In app delegate, you can try to add this function taking should rotate as global value and whenever you apply the orientation lock, we can make should rotate as false, it will be in portrait mode. If you don't apply orientation lock, we can make rotate as true, then it can rotate in all states.
Related
i have change the orientation from portrait to landscape in sub view controller using the below code
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
but when I go to background and return to foreground, then I press button to go to the home page then need change orientation from landscape to portrait using the below code
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if ([ConfigObjC currentPage] && [[ConfigObjC currentPage] isEqualToString:#"SubViewController"])
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
else{
return UIInterfaceOrientationMaskPortrait;
}
}
but it not work.
i have successful to change orienration from landscape to portrait when i no go to background. how can i fix this problem thanks very much
If you would like to place your windows in landscape mode, you can modify Xcode info to support landscape only,like this:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
Another way by coding is to change orientation of UIWindow, like this:
UIApplication *application=[UIApplication sharedApplication];
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
application.keyWindow.transform=CGAffineTransformMakeRotation(M_PI);
Using this way above needs to set ShouldAutoRotate with NO.
- (BOOL)shouldAutorotate{
return NO;
}
Besides, if there are only several views displaying in landscape mode , you can use the property transform to rotate these views, like this:
//rotate screen only with view
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
Hope can help you!
My game is implemented in a single SKScene object. A button (SKSpriteNode) lets the user switch from landscape to portrait mode. Well, it should. But I can't make this work. There are many discussions on this but they are all rather complicated and give suggestions that are completely different.
So.... What is the easiest way to make a button in an SKScene that changes the orientation to portrait or landscape?
Any suggestions are appreciated.
Here is a short, concise, complete answer to the question I posted.
In xcode, select File->New->Project->IOS->Application->Game to generate a default app framework for a game for IPad using SpriteKit with ObjectiveC. This is the classic HelloWorldApp where spinning airplanes appear whenever you touch the screen.
In Targets->DEploymentInfo->DeviceOrientation, checkmark Portrait and LandscapeLeft
Modify shouldAutorotate for the view controller like so...
- (BOOL)shouldAutorotate
{
int currentOrientation = (int)[[UIDevice currentDevice] orientation];
if (scene.orientation != currentOrientation) {
// Rotate the device back to orginial orientation
[[UIDevice currentDevice] setValue:
[NSNumber numberWithInteger: scene.orientation]
forKey:#"orientation"];
return NO;
}
return YES;
}
change the body of touchesBegan in your gameScene class like so...
-(void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
/* Called when a touch begins */
NSLog(#"%d", self.orientation);
if (!self.orientation || self.orientation == UIInterfaceOrientationPortrait) {
self.orientation = UIInterfaceOrientationLandscapeLeft;
} else {
self.orientation = UIInterfaceOrientationPortrait;
}
[[UIDevice currentDevice] setValue:
[NSNumber numberWithInteger: self.orientation]
forKey:#"orientation"];
the app will then hange orientation on each tap.
after browsing through stackoverflow for answers I decided to ask a question.
From my understanding, I'm supposed to override the supportedInterfaceOrientation to handle orientation. For example's sake I implemented it like this
- (NSUInteger)supportedInterfaceOrientations {
if (self.forceLandscape) {
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
This lets the controller start in landscape mode when presented and get the forceLandscape ON on default. Then there's a button that will change the orientation on button press
- (IBAction)buttonPress:(id)sender {
self.forceLandscape = !self.forceLandscape;
UIInterfaceOrientation o = UIInterfaceOrientationPortrait;
if (self.forceLandscape) {
o = UIInterfaceOrientationLandscape;
}
[UIApplication sharedApplication].statusBarOrientation = o;
}
Button press would alternatively change between portrait and landscape mode. By setting the status bar orientation it would call the supportedInterfaceOrientations to change the orientation for me. It does call the method and return mask portrait on first button press but it doesn't change the orientation for me. This is the issue I want to fix. Hope that there's a workaround for this.
Replacing the status bar orientation change to this code
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: o] forKey:#"orientation"];
Does call supportedInterfaceMethod and it does change the orientation. However it only work once and that it has access to private code and will be rejected by Apple, which is not desirable.
Not sure that this solution works. In my project (iOS6,7), I fixe the orientation so I don't need to force to change the orientation. However, I found a function in UIViewController that "attemp to rotate the device orientation to the right one
-(BOOL)shouldAutorotate {
FLog(#"");
if (self.forceLandscape) { //force to landscape
return NO;
} else {
return YES; //let's application rotate it self
}
}
-(NSUInteger)supportedInterfaceOrientations {
if (self.forceLandscape) {
return UIInterfaceOrientationMaskLandscapeLeft;
} else {
//You can just allow Portrait.
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskPortrait;
}
}
// Notifies when rotation begins, reaches halfway point and ends.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
//save new orientation
_myOrientation = toInterfaceOrientation;
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (IBAction)buttonPress:(id)sender {
self.forceLandscape = !self.forceLandscape;
if (self.forceLandscape) {
_myOrientation = UIInterfaceOrientationMaskLandscapeLeft
} else {
//just do nothing
}
//call this to update orientation
[UIViewController attemptRotationToDeviceOrientation];
}
I have an application that supports only landscape mode. When I opening the camera the app crashes. I solved the problem by using the following code.
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate {
return [[UIDevice currentDevice] orientation] != UIInterfaceOrientationPortrait;
}
But the app turns to support all orientations. All I want is the app only support landscape mode and also uses camera.
Any help will be appreciated.
I finally figured it out. The shouldAutorotate never gets called in ViewControllers. Because the topmost view controller has to return the interface orientations it wants to rotate to. In my case, that's the UINavigationController. So I creates the new category for UINavigationController to make it work. I uses the following methods in the category
#implementation UINavigationController (BugiOSFix)
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate {
return [[UIDevice currentDevice] orientation] != UIInterfaceOrientationPortrait;
}
#end
Write this For orientations .........
-(void)viewWillAppear:(BOOL)animated
{
[self willAnimateRotationToInterfaceOrientation:
[UIApplication sharedApplication].statusBarOrientation duration:1.0];
UIInterfaceOrientation orientation =
[UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeRight ||
orientation == UIInterfaceOrientationLandscapeLeft)
{
[self setFrameForLeftAndRightOrientation];
}
else if(orientation == UIInterfaceOrientationPortrait )
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
- (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[self setFrameForLeftAndRightOrientation];
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
-(void)setFrameForLeftAndRightOrientation
{
if(IS_IPAD)
{
// Write here for left and right orientation
}
else if(is_iPhone)
{
// Write here for iPhone land scape ;
}
-(void)setFrameForPortraitAndUpsideDownOrientation
{
if(IS_IPAD)
{
// Write here for Portrait orientation for iPad
}
else if(is_iPhone)
{
// Write here for iPhone Portrait orientation ;
}
}
See my answer at How to create a landscape-view only ios application for a similar question. All you have to do is to implement one method and your app will be forced to autorotate to the direction you want it.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
This will force your app to rotate to the UIInterfaceOrientationLandscapeLeft direction by providing your ViewController with only one preferred orientation.
I need to support only portrait orientation in my app. How can I do it? I have tried adding
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
and
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationPortrait;
}
But it does't help - the view rotates anyway.
Thanks in advance!
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientationshould return a BOOL which is NOin your case.
This should be:
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationPortrait
|| interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ;
}
In XCode 5 (not sure about earlier versions), if you want on support portrait mode only across the whole app, go to Project Settings->General->Deployment info
and uncheck Upside Down, Orientation Left, and Orientation Right.
This is probably the easiest way to do it now.
For iOS 6, it seems you need to add/override this method too,
-(BOOL)shouldAutorotate {
return NO;
}