在启动期间出现设备方向和UIView问题 [英] Issue with device orientation and UIView during startup

查看:109
本文介绍了在启动期间出现设备方向和UIView问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on a project in which when the user press a button a video will be played, I have to program this app for all orientations. I have added images as background for buttons given those buttons position based on the size of the superview like this:

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));

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

When I start the app in portrait it's view is fine and also when I rotate it works fine :

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

But if I start my app in landscape it shows a view like this:

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

I'v used a lot of code to resize views and subviews:

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

and in didRotateFromInterfaceOrientation() and 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。我在这里缺少什么?任何帮助将不胜感激。

I'v also specified YES for return in shouldAutoRotate(). What am I lacking here? Any help will be appreciated.

推荐答案

 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
    {

    }
}

}

这篇关于在启动期间出现设备方向和UIView问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆