在启动期间出现设备方向和UIView问题

时间:2013-11-15 11:54:02

标签: ios iphone objective-c uiview uiinterfaceorientation

我正在开展一个项目,当用户按下按钮时将播放视频,我必须为所有方向编写此应用程序。我添加了图像作为按钮的背景,根据超视图的大小给出了这些按钮的位置,如下所示:

button1.frame = CGRectMake(0.15 * (self.view.frame.size.width),0.15 * (self.view.frame.size.height), 0.2 * (self.view.frame.size.width), 0.2 * (self.view.frame.size.height));

当我以肖像方式启动应用程序时,它的视图很好,而且当我旋转它时工作正常:

screenshot portrait

但如果我在横向启动我的应用程序,它会显示如下视图:

screenshot landscape

我使用了大量代码来调整视图和子视图的大小:

-(void) viewDidLoad
{

...
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

[self.view setAutoresizesSubviews:YES];

button1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
...

}

和didRotateFromInterfaceOrientation()和willRotateToInterfaceOrientation():

...
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin |
        UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
        UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

    [self.view setAutoresizesSubviews:YES];

    button1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin |
        UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
        UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    ...

我还在shouldAutoRotate()中返回指定YES。我在这里缺少什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

 you have to declaration below and all give them in viewDidLoad but frame give in
 this code  


-(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)
     {

     }
     else if(IS_IPHONE)
     {

        if ([[UIScreen mainScreen] bounds].size.height == 568)
       {


       }
       else
      {

      }
    }

}

 -(void)setFrameForPortraitAndUpsideDownOrientation
 {
   if(IS_IPAD)
    {

     }
    else if(IS_IPHONE)
    {


    if ([[UIScreen mainScreen] bounds].size.height == 568)
    {


    }
    else
    {

    }
}

}