ios 无法将出口连接到 IB 中的约束

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22086054/
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-08-30 23:36:34  来源:igfitidea点击:

Unable to make outlet connection to a constraint in IB

iosinterface-buildernslayoutconstraintoutlet

提问by johnnyspo

I am running XCode 5 with iOS 7. I need to dynamically change the height constraint of a UITextView prior to its layout. I have seen many postings stating that this can be done by making an outlet connection from the constraint in IB to the source code. I am trying to make the connection from the constraint outlet in IB to the header file of the owning class, but am unable to make the connection - IB does not "highlight" when I drag over the header file, as it does when making connections from other objects like buttons etc.

我在 iOS 7 上运行 XCode 5。我需要在布局之前动态更改 UITextView 的高度约束。我看到很多帖子都说这可以通过从 IB 中的约束到源代码建立一个出口连接来完成。我试图建立从 IB 中的约束出口到所属类的头文件的连接,但无法建立连接 - 当我拖过头文件时,IB 不会“突出显示”,就像在建立连接时一样来自其他对象,如按钮等。

Has anyone encountered this before? All help is greatly appreciated.

有没有人遇到过这个?非常感谢所有帮助。

回答by matt

Here is a picture of me doing it. I'm control-dragging from the constraint to the header file.

这是我做的照片。我正在控制从约束拖动到头文件。

enter image description here

在此处输入图片说明

In real life I'm more likely to control-drag to the anonymous category in the implementation file, because I don't want an IBOutlet property to be public. So here's a picture of that too. Sorry, this screen shot does not show the cursor: it is at the bottom end of the stretch line.

在现实生活中,我更有可能控制拖动到实现文件中的匿名类别,因为我不希望 IBOutlet 属性是公开的。所以这里也有一张照片。抱歉,此屏幕截图未显示光标:它位于拉伸线的底端。

enter image description here

在此处输入图片说明

回答by motox

You can make an outlet connection by:

您可以通过以下方式进行插座连接:

  1. Opening the assistant editor and then ctrl+drag your textview to the respective header file. You should see a property created for the textview.

  2. Manually type @property(nonatomic,retain)IBOutlet UITextField *yourTextField;.Open the assistant editor, drag from the small empty circle(left to the property) to the corresponding control in your view controller.

  1. 打开辅助编辑器,然后 ctrl+drag 你的 textview 到相应的头文件。您应该会看到为 textview 创建的属性。

  2. 手动输入 .@property(nonatomic,retain)IBOutlet UITextField *yourTextField;打开辅助编辑器,从小空圆圈(属性左侧)拖动到您的视图控制器中的相应控件。

If you are still not able to make the connection, clean, save your project and then try again.

如果您仍然无法建立连接,请清理并保存您的项目,然后重试。

回答by David

Are you trying to create the connection in a UIView class? It seems you can only create a connection in the UIViewController class that is associated with that Scene in your Storyboard.

您是否尝试在 UIView 类中创建连接?似乎您只能在与故事板中该场景关联的 UIViewController 类中创建连接。

So your workaround is probably the right thing if you wish to do it from the View itself.

因此,如果您希望从视图本身执行此操作,那么您的解决方法可能是正确的。

回答by Giuseppe Barbalinardo

I think I am late for this answer but it still may be worth to write it. I had a similar problem. I hadn't set up the file owner for the view correctly. You can do a simple test using the assistant editor. If the assistant editor doesn't let you pick the automaticselection for the view controller it means that you didn't set up the file owner correctly. Check that the viewcontroller has the same name of the .xib file (that's often the case).

我想我为这个答案迟到了,但仍然值得写它。我有一个类似的问题。我没有正确设置视图的文件所有者。您可以使用辅助编辑器进行简单的测试。如果助理编辑器不允许您为视图控制器选择自动选择,则意味着您没有正确设置文件所有者。检查视图控制器是否与 .xib 文件具有相同的名称(通常是这种情况)。

This will let you connect all your IBOutlets, including constraints.

这将让您连接所有 IBOutlets,包括约束。

回答by johnnyspo

Ok, still can't make the connection to the constraint in IB. However, I worked around the problem by making the text view height constraint a placeholder in IB that I replace with an NSLayoutConstraint that I create in the view's initWithCoder. I maintain a reference to the created constraint and modify its "constant" property as needed.

好的,仍然无法连接到 IB 中的约束。但是,我通过使文本视图高度约束成为 IB 中的占位符来解决这个问题,我将其替换为我在视图的 initWithCoder 中创建的 NSLayoutConstraint。我维护对创建的约束的引用并根据需要修改其“常量”属性。

I'll probably stick with this workaround, but if anyone can help me with the original problem that would be greatly appreciated.

我可能会坚持使用这种解决方法,但如果有人能帮助我解决原始问题,将不胜感激。