简体   繁体   English

如何在iPhone中设置设备方向?

[英]How to set the device orientation in iphone?

I am doing a project which supports both landscape mode and portrait mode,but now i am struck there is no orentation in my project.I set the 我正在做一个同时支持横向和纵向模式的项目,但是现在我很惊讶,我的项目中没有任何约束。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    //return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
    return YES;
}

but no use,when i press the command +Right-arrow for rotation to right it rotates to left but view and controllers didnt change to that orentation.Then after some googling i get this code 但是没有用,当我按命令+ Right-arrow向右旋转时,它向左旋转,但是视图和控制器没有更改为该方向。然后经过一些谷歌搜索后,我得到了此代码

if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
    // do stuff
}

But the problem is i didn't know how to write code in //do stuff.Please help me to do this. 但问题是我不知道如何用// do编写代码。请帮助我做到这一点。 thanks in advance. 提前致谢。 EDIT: i put this code ,it is working but whenthe simulator is again turns to portrait it wont be in the default mode.my code is 编辑:我把这段代码,它正在工作,但当模拟器再次变为纵向时,它将不在默认模式下。我的代码是

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    {
        if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
        {
            NSLog(@"Csantos shouldAutorotateToInterfaceOrientation: left or right");
            //
            table.transform = CGAffineTransformIdentity;
            table.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
            table.bounds = CGRectMake(0.0, 0.0, 320, 402);//[self.view setFrame:CGRectMake(0.0, 0.0, 768, 90)];
        }
        else
        {
            table.transform = CGAffineTransformIdentity;
            table.transform = CGAffineTransformMakeRotation(degreesToRadian(360));
            table.bounds = CGRectMake(0.0, 51, 320, 402);
        }
               return YES;
   }*/
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    {
        //Handle portrait
    }
    else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
     {
         //Handle landscape
     }
}

This is better code. 这是更好的代码。

You can do like this. 你可以这样

if (UIInterfaceOrientationPortrait == interfaceOrientation || UIInterfaceOrientationLandscapeLeft == interfaceOrientation || UIInterfaceOrientationLandscapeRight == interfaceOrientation ||
    UIInterfaceOrientationPortraitUpsideDown == interfaceOrientation) {
    return  YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM