Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何对UIView进行自定义布局,使其既适用于iPhone 5屏幕尺寸,也适用于常规屏幕尺寸_Ios_Objective C_Layout_Autoresizingmask_Contentmode - Fatal编程技术网

Ios 如何对UIView进行自定义布局,使其既适用于iPhone 5屏幕尺寸,也适用于常规屏幕尺寸

Ios 如何对UIView进行自定义布局,使其既适用于iPhone 5屏幕尺寸,也适用于常规屏幕尺寸,ios,objective-c,layout,autoresizingmask,contentmode,Ios,Objective C,Layout,Autoresizingmask,Contentmode,以下是期望的结果。蓝色区域是感兴趣的视图。UIView不是UIImageView 我尝试过各种各样的自动调整屏幕大小的方法,但都没有用处理不同的屏幕大小可能会很棘手。在您的情况下,它不是:) 由于您想在屏幕上居中显示任意大小的视图,因此只需将视图的中心设置为屏幕的中心即可 CGRect screenBounds = [[UIScreen mainScreen] bounds]; view.center = CGPointMake(screenBounds.size.width/2,screen

以下是期望的结果。蓝色区域是感兴趣的视图。UIView不是UIImageView


我尝试过各种各样的自动调整屏幕大小的方法,但都没有用

处理不同的屏幕大小可能会很棘手。在您的情况下,它不是:) 由于您想在屏幕上居中显示任意大小的视图,因此只需将视图的中心设置为屏幕的中心即可

CGRect screenBounds = [[UIScreen mainScreen] bounds];
view.center = CGPointMake(screenBounds.size.width/2,screenBounds.size.height/2);
此代码假定视图的superView边界与screenBounds的大小相同。

首先:子类UIView(创建MYUIView)。 第二:重写方法

- (void)layoutSubviews {
   [super layoutSubviews];
   // .. put your code...
}
并通过读取屏幕大小在该方法内手动执行帧更新。
自动调整掩码大小必须设置为UIViewAutoresizingNone。

这只能通过编程实现。一个选项是@user2223761建议的子类化。如果不想将UIView子类化,则需要在方向更改时设置框架,并将yourView.center设置为中心的中心

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {

   if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
          // Make sure that the frame is centered in the screen
          NSInteger paddingLeftSide = (self.view.bounds.size.width - 480) / 2;
          self.view.frame = CGRectMake(paddingLeftSide, 0, 480, 320);   
      } else {
          self.view.frame = CGRectMake(0, 0, 320, 320);
      }
  }

为此,您必须以编程方式设置框架。我想知道是否有一种方法可以用最少的代码来完成。甚至会考虑使用自动布局/约束,如果它能解决这个问题,但我不确定是否有可能首先提到在所有视图中你想要的尺寸是多少?在iphone4和iphone5横向和纵向模式下。写下视图的大小图片中列出了iPhone 4和iPhone 5在纵向和景观中的所有大小。请添加一些说明,说明为什么这是问题的答案,以便其他读者了解这里发生了什么。嘿,大卫,我明白了这是如何使视图居中的。我可以称之为每一次方向的改变。然而,为了使其达到正确的大小,您是否建议我也必须在每次旋转时设置帧?谢谢用户,我想知道是否有一种方法可以用最少的代码来完成。甚至会考虑使用自动布局/约束,如果它能解决这个问题,但我不确定它会。