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

标签 ios iphone objective-c uiview uiinterfaceorientation

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

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

当我以纵向模式启动应用程序时,它的 View 很好,而且当我旋转它时它也可以正常工作:

screenshot portrait

但如果我以横向方式启动我的应用程序,它会显示如下 View :

screenshot landscape

我使用了很多代码来调整 View 和 subview 的大小:

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

最佳答案

 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
    {

    }
}

}

关于ios - 启动期间设备方向和 UIView 出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20000503/

相关文章:

iphone - 为什么 GCD 代码在 drawRect 中崩溃?

iphone - 0 不适用于货币类型输入

ios - 如何在 iOS 的触地得分上添加不透明度?

iphone - 我如何解决这个 Objective-C 类中双向依赖的问题?

objective-c - 使用 vararg 格式化 Kotlin Native iOS 字符串

ios - Obj-c - 如果没有搜索结果,则在表格 View 中返回 1 个单元格?

ios - 从 Objective C View Controller 引用 Swift View Controller

objective-c - Cocoa 对象的链表

iphone - 我怎样才能使 deleteRowsAtIndexPaths : work with GenericTableViewController?

ios - Images.xcassets 为 iPhone 6 拍摄了错误的图像?