如何避免 Xcode 对情节提要文件进行无偿编辑?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5900245/
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
How to avoid Xcode gratuitous edits to storyboard files?
提问by danh
When I navigate to one of my xib files, Xcode marks the file as touched. Undo and revert have no effect. Saving seems to do no harm, but the glitch causes me frequent additional git work.
当我导航到我的一个 xib 文件时,Xcode 将该文件标记为已触摸。撤消和还原没有效果。保存似乎没有坏处,但故障导致我经常进行额外的 git 工作。
Has anyone else seen this or have an idea about something in my MainWindow.xib that would cause it?
有没有其他人看到过这个或者对我的 MainWindow.xib 中的某些东西有什么想法会导致它?
Edit in 2019 - still happening!
2019 年编辑 - 仍在发生!
采纳答案by danh
Apple says it's a known issue:
Apple 表示这是一个已知问题:
This is a follow up to Bug ID# 9847336. After further investigation it has been determined that this is a known issue, which is currently being investigated by engineering. This issue has been filed in our bug database under the original Bug ID# 9056156. The original bug number being used to track this duplicate issue can be found in the State column, in this format: Duplicate/OrigBug#.
这是对错误 ID#9847336 的跟进。经过进一步调查后,确定这是一个已知问题,目前正在由工程部门进行调查。此问题已在我们的错误数据库中以原始错误 ID# 9056156 归档。用于跟踪此重复问题的原始错误编号可以在状态列中找到,格式如下:Duplicate/OrigBug#。
回答by Adam Eisfeld
After attempting many different solutions to get around this extremely annoying bug, I've found one method that works (it is certainly not a "fix", but a workaround).
在尝试了许多不同的解决方案来解决这个极其烦人的错误之后,我找到了一种有效的方法(它当然不是“修复”,而是一种解决方法)。
Lock controllers when you're done with them.
完成后锁定控制器。
Select a view controller in a storyboard, open the Identity inspector, and at the bottom in the Document section, you'll see a Lock dropdown. Select "All Properties" from the lock drop down.
在故事板中选择一个视图控制器,打开身份检查器,在文档部分的底部,您将看到一个锁定下拉列表。从锁定下拉菜单中选择“所有属性”。
If you do this for all of the controllers giving you issues (or just all of the controllers in general, even), you'll notice when you open the storyboard, Xcode will still modify the frames of various elements, but then immediately undo the changes.
如果你对所有给你带来问题的控制器(或者只是所有的控制器,甚至)都这样做,你会注意到当你打开故事板时,Xcode 仍然会修改各种元素的框架,但随后立即撤消变化。
This then leads to the development cycle being:
这导致开发周期为:
- Unlock a view controller
- Perform any necessary changes
- Lock the view controller
- Commit
- 解锁视图控制器
- 执行任何必要的更改
- 锁定视图控制器
- 犯罪
Locking also works on a view-by-view case if that suits your needs better.
如果更适合您的需求,锁定也适用于逐个视图的情况。
The bug has been around for quite some time now and it doesn't appear to be getting fixed any time soon, so unfortunately this will have to suffice for now.
该错误已经存在了相当长的一段时间,而且似乎不会很快得到修复,因此不幸的是,现在这已经足够了。
回答by mikeho
I personally feel that knowing that it's a known bug doesn't exactly fix the issue. Here's what I figured out so far and how to keep them from coming up.
我个人认为,知道这是一个已知错误并不能完全解决问题。这是我到目前为止所想到的以及如何防止它们出现的方法。
NOTE: This is for a storyboard with AutoLayout enabled.I haven't yet figured it out yet for manual storyboards.
注意:这是用于启用 AutoLayout 的故事板。对于手动故事板,我还没有弄清楚。
Below is an example of a button.
下面是一个按钮的例子。
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Ebd-ny-Ill">
<rect key="frame" x="97" y="5" width="135" height="30"/>
<color key="backgroundColor" white="0.94557291669999999" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="width" constant="86" id="bXV-mS-ksp">
<variation key="heightClass=regular" constant="135"/>
</constraint>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="13"/>
<state key="normal" title="See Full Menu">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<variation key="default">
<mask key="constraints">
<exclude reference="bXV-mS-ksp"/>
</mask>
</variation>
<variation key="heightClass=regular" misplaced="YES">
<rect key="frame" x="97" y="5" width="135" height="28"/>
<mask key="constraints">
<include reference="bXV-mS-ksp"/>
</mask>
</variation>
<connections>
<action selector="showMenu:" destination="eQk-yf-uDh" eventType="touchUpInside" id="xOP-Se-FXQ"/>
</connections>
</button>
If you take a look, you'll see that the button has an element called <rect/>
This defines the size and position of the UI item. If you look below, you'll also see an element called <variation/>
that describes the change in position based on the size class. In order to fix the position for this, we have to use the following steps:
如果您看一下,您会看到该按钮有一个名为<rect/>
This的元素,它定义了 UI 项的大小和位置。如果您往下看,您还会看到一个名为的元素<variation/>
,它描述了基于尺寸类别的位置变化。为了固定这个位置,我们必须使用以下步骤:
- Take a look at the warning that shows up in Xcode. Take note of what needs to be changed for that UI element.
- Open the storyboard in an external (text) editor.
- Find the UI element in the storyboard.
- Change the value to match what the warning is suggesting in both
<rect/>
and<variation/>
. - Save the file in the external editor.
- Go back to Xcode. Let it re-calculate all the constraints.
- Accept the warning for the UI element that pops up again.
- If you go back to the external editor, you'll notice that the
<variation/>
element is gone.
- 查看 Xcode 中显示的警告。记下该 UI 元素需要更改的内容。
- 在外部(文本)编辑器中打开故事板。
- 在故事板中找到 UI 元素。
- 更改值以匹配警告在两个提示
<rect/>
和<variation/>
。 - 将文件保存在外部编辑器中。
- 回到Xcode。让它重新计算所有的约束。
- 接受再次弹出的 UI 元素的警告。
- 如果您返回外部编辑器,您会注意到该
<variation/>
元素已消失。
In my example, I had to update x="97"
to x="597"
.
在我的示例中,我必须更新x="97"
到x="597"
.
I'm still working on elements that don't have a <variation/>
. I'll update this answer as soon as I figure it out.
我仍在研究没有<variation/>
. 我会在弄清楚后立即更新此答案。