(1)先了解一下这几个枚举值的含义:

AutoresizingMask 的使用

 

(2)代码演说:

在viewcontroller 用代码创建一个红色的view,如下:

    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    CGFloat marginX = self.view.frame.size.width - 100;
    CGFloat marginY = self.view.frame.size.height - 100;
    redView.frame = CGRectMake(marginX, marginY, 100, 100);
    [self.view addSubview:redView];

运行完后的效果图如下:

 

AutoresizingMask 的使用

 

让手机横屏显示后的效果如下:

AutoresizingMask 的使用

 

红色的视图看不到了,因为横屏显示后,并没有重新给redView这个视图重新布局;但是如果加上AutoresingMask后,代码如下:

   UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    CGFloat marginX = self.view.frame.size.width - 100;
    CGFloat marginY = self.view.frame.size.height - 100;
    redView.frame = CGRectMake(marginX, marginY, 100, 100);
    [self.view addSubview:redView];
    
    redView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;

运行的后,切换横屏的效果如下:

AutoresizingMask 的使用

 

redView这个视图不仅固定在底部,而且还根据屏幕的宽度修改了redView的宽度。

 

但是antoresizing只能解决父控件与子控件之间的相对关系。

 

相关文章:

  • 2021-10-23
  • 2021-04-24
  • 2021-11-19
  • 2021-05-21
  • 2022-12-23
  • 2021-12-05
猜你喜欢
  • 2021-06-19
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
相关资源
相似解决方案