简体   繁体   English

自动调整大小不起作用

[英]Autoresizing not working

I am facing a problem I really do not understand. 我面临一个我真的不明白的问题。

I have a view (grey rectangle) and a subview (red rectangle). 我有一个视图(灰色矩形)和一个子视图(红色矩形)。 The architecture is UIViewController => View(grey rectangle) => UIImageView (red rectangle). 该体系结构是UIViewController => View(灰色矩形)=> UIImageView(红色矩形)。

I want the red rectangle to resize when the grey rectangle bounds change. 我希望灰色矩形范围更改时,红色矩形能够调整大小。

So I put the property "Autoresize subviews" to Yes for the grey rectangle and add the following code : 因此,我将灰色矩形的属性“ Autoresize subviews”设置为“是”,并添加以下代码:

_image.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ;

But when I resize the grey rectangle, nothing happens for the red one : the grey one is bigger and the red one keeps the same frame. 但是当我调整灰色矩形的大小时,红色的矩形什么也没有发生:灰色的矩形更大,红色的矩形保持相同的帧。

Do you have any idea to help me ? 你有什么主意要帮我吗?

Update : 更新:

I added the imageView in the storyboard as subview of the "grey rectangle". 我在情节提要中将imageView添加为“灰色矩形”的子视图。 I tried to add another UIImageView by code and that works. 我试图通过代码添加另一个UIImageView,并且有效。

So my new question is do you know why autoresizing is not working with subviews edited in storyboard ? 因此,我的新问题是,您是否知道为什么自动调整大小不适用于在情节提要中编辑的子视图?

尝试这个:

_image.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; 

If you are willing to use Auto layout, your problems can be solved using the following steps: 如果您愿意使用自动版式,则可以使用以下步骤解决您的问题:

  1. In the Storyboard, select the view - go to "file inspector" and check the box named "Use Autolayout" 在情节提要中,选择视图-转到“文件检查器”,然后选中名为“使用自动布局”的框
  2. Add the following constraints: 添加以下约束:
    • Top space to container 容器的顶部空间
    • Bottom space to container 容器的底部空间
    • Leading space to container and 通往集装箱的领先空间
    • Trailing space to container 拖到集装箱的空间

This will - 这将 -

  1. Just work 刚刚工作
  2. Will be sustainable even when Autoresizing will no longer be used. 即使不再使用自动调整大小也将是可持续的。

Edit 编辑


Check if the auto layout is turned off. 检查自动布局是否关闭。 If it is not, and you are trying to make the autoresize work through code, you will need to do the following for all the views you want to work with your autoresizing masks: 如果不是,并且您尝试通过代码使自动调整大小起作用,则需要对要使用自动调整大小掩码的所有视图执行以下操作:

self.view.translatesAutoresizingMaskIntoConstraints = YES;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM