每次 Xcode 重新启动时都会出现错位的视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29289892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:52:42  来源:igfitidea点击:

Misplaced views each time Xcode restarts

iosobjective-cxcodeinterface-builderautolayout

提问by djibouti33

I'm using storyboards for the first time in my app

我第一次在我的应用中使用故事板

When I close Xcode, I have 0 warnings about misplaced views or anything pertaining to my storyboard. When I restart Xcode and open the storyboard, 3 out of my 15 scenes have misplaced view warnings.

当我关闭 Xcode 时,关于错误放置的视图或与我的故事板有关的任何内容,我都会收到 0 条警告。当我重新启动 Xcode 并打开故事板时,我的 15 个场景中有 3 个有错位的视图警告。

Without touching anything (other than selecting the file), I can issue a git statusand see that the .storyboard file has changes. If I git diff, I see this included in multiple areas of the file:

不接触任何东西(除了选择文件),我可以发出 agit status并看到 .storyboard 文件有变化。如果 I git diff,我看到这包含在文件的多个区域中:

<variation key="widthClass=compact" misplaced="YES">
    <rect key="frame" x="8" y="56" width="130" height="34"/>
</variation>

If an element already had <variation key="widthClass=Compact"node, then the misplacedattribute and rectnode are added.

如果元素已经有<variation key="widthClass=Compact"节点,则添加misplaced属性和rect节点。

The only thing I have to do to get the warnings to go away is click on each one, make sure "Update Frame" is selected, and click "Fix Misplacement". That fixes everything until I restart Xcode.

要消除警告,我唯一要做的就是单击每个警告,确保选择了“更新框架”,然后单击“修复错位”。这修复了一切,直到我重新启动 Xcode。

The basic structure of my scene is:

我的场景的基本结构是:

UIView
    UICollectionView
        UICollectionReusableView
        UICollectionViewCell
        UICollectionViewCell
    UISegmentedControl

How can I prevent this from happening each time I restart?

每次重新启动时如何防止这种情况发生?

回答by Sushree Swagatika

This generally happens when you have not set the constraints properly.

当您没有正确设置约束时,通常会发生这种情况。

Try removing all views inside the UIViewController and adding newly again along with your constraints.

尝试删除 UIViewController 中的所有视图并重新添加新的约束。

Clean and run your code.

清理并运行您的代码。

回答by SHN

One possible reason for misplaced views is: If you have used live views(IBDesignables) in storyboards then if you open the storyboard and quickly move to some other file before the IB has finished building all the live views then the the views get misplaced. The IB starts building the live views as soon as you view the story board in the editor. To fix the misplaced views, allow IB some time to finish building the storyboard by staying on the storyboard for few moments, and the misplaced views will get fixed.

视图错位的一个可能原因是:如果您在故事板中使用了实时视图 (IBDesignables),那么如果您在 IB 完成所有实时视图的构建之前打开故事板并快速移动到某个其他文件,则视图会错位。一旦您在编辑器中查看故事板,IB 就会开始构建实时视图。要修复错位的视图,请允许 IB 一些时间通过在故事板上停留片刻来完成构建故事板,错位的视图将得到修复。

回答by SoftDesigner

In my case it happens for all labels / buttons with custom fonts and intrinsic (not explicitly defined) sizes. Looks like an Xcode bug.

在我的情况下,所有具有自定义字体和内在(未明确定义)大小的标签/按钮都会发生这种情况。看起来像一个 Xcode 错误。

回答by X140Yu

I face the same issue before, and I suppose it's not our fault, it's just a Xcode's bug.

我以前也遇到过同样的问题,我想这不是我们的错,只是 Xcode 的错误。

If your .storyboardfile changed but you didn't touch anything, just select the file and select the Xcode menu Source Control-> Discard Changes in "xxxx.storyboard"..., it should be work :)

如果您的.storyboard文件已更改但您没有触及任何内容,只需选择该文件并选择 Xcode 菜单Source Control-> Discard Changes in "xxxx.storyboard"...,它应该可以工作:)

回答by priya bagrecha

In the bottom of the storyboard we have the option to select the size class. If I change the option from wRegular hRegular to any other option I get the similar issue. I don't know the reason for this.

在故事板的底部,我们可以选择尺寸等级。如果我将选项从 wRegular hRegular 更改为任何其他选项,我会遇到类似的问题。我不知道这是什么原因。

Try to resolve it by selecting the same size class after opening the project as you have selected before closing the project.

尝试通过在打开项目后选择与您在关闭项目前选择相同的大小类来解决它。

For example, if you have selected the size class as wCompact hRegular for iPhone and you have closed your project. After opening the project again, you will see that the size class has changed to wRegular hRegular. Because of this the view objects change. Select wCompact hRegular again to resolve the issue.

例如,如果您为 iPhone 选择了尺寸等级为 wCompact hRegular 并且您已经关闭了您的项目。再次打开工程,你会看到size class变成了wRegular hRegular。因此,视图对象会发生变化。再次选择 wCompact hRegular 以解决该问题。

Hope it will help.

希望它会有所帮助。

回答by Sameh Salama

This issue is caused when you add constraints that don't fit the actual screen size at run time. for example : if you have a UIButton with width that exceeds screen width (let's say width 600) and you add fixed width constraint to that UIButton but no leading or trailing constraints , Xcode won't give you warnings as there are no conflicts, but when you actually run the app on a device or simulator with screen width less than 600, Xcode will automatically adjust UIButton width to fit the screen width which is less than 600, causing your layout constraints to change to what fitted the screen at runtime

当您在运行时添加不适合实际屏幕尺寸的约束时会导致此问题。例如:如果您有一个宽度超过屏幕宽度(假设宽度为 600)的 UIButton 并且您向该 UIButton 添加固定宽度约束但没有前导或尾随约束,Xcode 不会给您警告,因为没有冲突,但是当您在屏幕宽度小于 600 的设备或模拟器上实际运行应用程序时,Xcode 会自动调整 UIButton 宽度以适应小于 600 的屏幕宽度,从而导致您的布局约束在运行时更改为适合屏幕的宽度

回答by Nupur Gupta

If you are using size classes then either your constraints are not up to the mark (according to all device type if Any-Any). If not then try to adjust the frame or constraints and check once again.

如果您使用的是尺寸等级,那么您的约束条件不符合标准(根据所有设备类型,如果是任意类型)。如果不是,则尝试调整框架或约束并再次检查。

Auto layout may also cause this issue. You will get all the information regarding the warning once you click on them.

自动布局也可能导致此问题。单击它们后,您将获得有关警告的所有信息。

回答by Nupur Gupta

I used to face the same problem with my previous app. What I noticed is that the size of storyboards matters here.

我以前的应用程序曾经遇到过同样的问题。我注意到故事板的大小在这里很重要。

This is Xcode issue.

这是 Xcode 问题。

If we use storyboard of any width any height, this problem doesn't occur. If you are using storyboards of size Compact width regular height, this issue occurs.

如果我们使用任何宽度任何高度的故事板,则不会发生此问题。如果您使用的是Compact width regular height大小的故事板,则会出现此问题。

So try to use default storyboard sizesin Xcode

所以尝试在 Xcode 中使用默认的故事板尺寸

回答by Longfei Wu

  1. click to preview a regular file (not .xibor .storyboardfile)
  2. quit Xcode
  3. run git checkout .
  4. restart Xcode
  1. 单击以预览常规文件(不是.xib.storyboard文件)
  2. 退出 Xcode
  3. git checkout .
  4. 重启Xcode

the .storyboardfile is not modified now.

.storyboard文件现在未修改。

回答by Allen

A simple fix may be to simply allow Xcode to choose constraints for you, then go back and check what it has chosen to be sure it will work for you.

一个简单的解决方法可能是简单地允许 Xcode 为您选择约束,然后返回并检查它选择的内容以确保它适合您。

In storyboard:

在故事板中:

  • Select View Controller giving you problems
  • Resolve Auto Layout Issues (bottom right)
  • Selected Views: Reset to Suggested Constraints
  • 选择 View Controller 给你带来问题
  • 解决自动布局问题(右下)
  • 选定的视图:重置为建议的约束

To check what constraints have been reset:

要检查已重置哪些约束:

  • Select each View within controller
  • Constraints Inspector (top right)
  • scroll down and examine constraints
  • 选择控制器中的每个视图
  • 约束检查器(右上角)
  • 向下滚动并检查约束