简体   繁体   English

无法与IB中的约束进行出口连接

[英]Unable to make outlet connection to a constraint in IB

I am running XCode 5 with iOS 7. I need to dynamically change the height constraint of a UITextView prior to its layout. 我正在使用iOS 7运行XCode 5.我需要在布局之前动态更改UITextView的高度约束。 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. 我看过许多帖子,说明这可以通过从IB中的约束到源代码建立出口连接来完成。 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. 我试图从IB中的约束出口连接到拥有类的头文件,但无法建立连接 - 当我拖动头文件时IB不会“突出显示”,就像在建立连接时一样从其他对象,如按钮等

Has anyone encountered this before? 有没有人遇到过这个? All help is greatly appreciated. 非常感谢所有帮助。

Here is a picture of me doing it. 这是我做这件事的照片。 I'm control-dragging from the constraint to the header file. 我控制 - 从约束拖动到头文件。

在此输入图像描述

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. 在现实生活中,我更有可能控制 - 拖动到实现文件中的匿名类别,因为我不希望IBOutlet属性是公共的。 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. 抱歉,此屏幕截图未显示光标:它位于拉伸线的底端。

在此输入图像描述

You can make an outlet connection by: 您可以通过以下方式建立出口连接:

  1. Opening the assistant editor and then ctrl+drag your textview to the respective header file. 打开助理编辑器,然后按住Ctrl键并将文本视图拖动到相应的头文件中。 You should see a property created for the textview. 您应该看到为textview创建的属性。

  2. Manually type @property(nonatomic,retain)IBOutlet UITextField *yourTextField; 手动输入@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. 。打开助手编辑器,从小空圆圈(左侧到属性)拖动到视图控制器中的相应控件。

If you are still not able to make the connection, clean, save your project and then try again. 如果仍无法建立连接,请清理,保存项目,然后重试。

Are you trying to create the connection in a UIView class? 您是否尝试在UIView类中创建连接? It seems you can only create a connection in the UIViewController class that is associated with that Scene in your Storyboard. 看来你只能在UIViewController类中创建一个与Storyboard中的那个Scene相关联的连接。

So your workaround is probably the right thing if you wish to do it from the View itself. 因此,如果您希望从View本身进行操作,那么您的解决方法可能是正确的。

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 automatic selection 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). 检查viewcontroller是否具有.xib文件的相同名称(通常是这种情况)。

This will let you connect all your IBOutlets, including constraints. 这将允许您连接所有IBOutlet,包括约束。

Ok, still can't make the connection to the constraint in IB. 好的,仍然无法连接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. 但是,我通过使文本视图高度约束成为IB中的占位符来解决此问题,我将替换为我在视图的initWithCoder中创建的NSLayoutConstraint。 I maintain a reference to the created constraint and modify its "constant" property as needed. 我保持对创建的约束的引用,并根据需要修改其“常量”属性。

I'll probably stick with this workaround, but if anyone can help me with the original problem that would be greatly appreciated. 我可能会坚持使用这种解决方法,但如果有人能帮助我解决原始问题,我将不胜感激。

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

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