自动旋转没有正确发生

时间:2013-04-01 05:43:39

标签: iphone ios ipad

我正在为iPad进行Autorotation,但我遇到了一个奇怪的问题。

问题是如果我开始使用ipad作为纵向视图并将ipad旋转到横向。它显示了肖像的大小而不是景观。以同样的方式,如果我开始使用ipad作为横向视图并将ipad旋转到纵向视图。它显示了景观的大小而不是肖像。我正在使用以下代码来更改对象的大小

- (void)viewDidLoad {

 UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
    if (interfaceOrientation == UIInterfaceOrientationLandscapeRight
        || interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        table.frame = CGRectMake(83, 600, 550, 275);
           im.frame = CGRectMake(0, 62, 775, 70);
          im1.frame = CGRectMake(0, 135, 775, 40);
        im2.frame = CGRectMake(60, 325, 650, 550);
         l17.frame = CGRectMake(0, 62, 775, 70);
        l16.frame = CGRectMake(85, 145, 780, 40);
           l15.frame = CGRectMake(83, 200, 600, 40);
        img.frame = CGRectMake(550, 300, 150, 110);
        l18.frame = CGRectMake(28, 550, 650, 40);
           b7.frame = CGRectMake(83, 880, 600, 40);
        //[self.view addSubview:table];

    }

    [super viewDidLoad];
}

请建议我哪里出错了?

2 个答案:

答案 0 :(得分:0)

-(void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {

    if ((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight))
        table.frame = CGRectMake(83, 600, 550, 275);
        im.frame = CGRectMake(0, 62, 775, 70);
        im1.frame = CGRectMake(0, 135, 775, 40);
        im2.frame = CGRectMake(60, 325, 650, 550);
        l17.frame = CGRectMake(0, 62, 775, 70);
        l16.frame = CGRectMake(85, 145, 780, 40);
        l15.frame = CGRectMake(83, 200, 600, 40);
        img.frame = CGRectMake(550, 300, 150, 110);
        l18.frame = CGRectMake(28, 550, 650, 40);
        b7.frame = CGRectMake(83, 880, 600, 40);
    else
        tableView.frame = CGRectMake(0,73,320,390);
        --
        --

}

你试过这样的吗?

答案 1 :(得分:0)

检查方向是否有效?

代码::

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    ......
}

检查方法::

-(void)orientationChanged {

   NSLog(@"Orientation Changed..!!");

   UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;

   //Lanscape View
   if (interfaceOrientation == UIInterfaceOrientationLandscapeRight
    || interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
   {
       table.frame = CGRectMake(83, 600, 550, 275);
       .....
   }

   //Portrait View
   else
   {
       table.frame = CGRectMake(x, y, w, h);
       .....
   }
}

方向更改方法代码

在PCH文件中,

#define IOS_OLDER_THAN_6        ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] < 6.0 )
#define IOS_NEWER_OR_EQUAL_TO_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0 )

在.m文件中,

//For Less than IOS 6

#ifdef IOS_OLDER_THAN_6
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation
{
   return toInterfaceOrientation;
}
#endif

// For Newer than IOS 6.
#ifdef IOS_NEWER_OR_EQUAL_TO_6
-(BOOL)shouldAutorotate
{
   return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
   return (UIInterfaceOrientationMaskAll);
}
#endif

希望对你有所帮助。

感谢。