Unable to make outlet connection to a constraint in IB
Asked Answered
O

5

24

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.

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

Ornamented answered 28/2, 2014 at 4:13 Comment(0)
F
53

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.

enter image description here

Flocculate answered 1/3, 2014 at 4:24 Comment(3)
Thanks matt. I am trying to do the exact same thing in my project, but XCode doesn't want to make the connection. I am going to create a test project and see if it will work in that project. Can't explain why XCode doesn't want to do it!Ornamented
Thanks matt!! You just saved me so much time!!Ashbaugh
So strange that cannot connect from File's Owner to IB except margin constraints, but this answer works well.Broadtail
P
3

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.

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

Pellucid answered 28/2, 2014 at 4:24 Comment(4)
Style points awarded if you make the connection to the class extension @interface section of your .m file instead of your .h Nobody else in your app needs to see that noise, define it in the .m where it belongs.Snowshed
Declaring in the @interface section of .h or .m file doesn't matter.Pellucid
motox - sorry for the confusion, I am trying to make a connection with a constraint not the text view. I edited my question to be clear. I can make the connection to the text view and other objects, I just cannot make the connection to the height constraint of the text view. I tried to clean the project, still can't make the connection. I have seen many posts indicating that this can be done, but for some reason I cannot do it. I have also tried your suggestion #2 (except for a constraint vs text view); still doesn't work.Ornamented
@Ornamented After you have inserted you UITextView in IB, you should be able to set your required constraints through "PIN" button located at the bottom. Constraint specifications doesn't matter in IB since you modify them dynamically. You should be able to see these constraints in Document Outline view. Now, go to the respective header file and type @property (weak, nonatomic) IBOutlet NSLayoutConstraint *yourTextViewHeight;. You can then drag the empty circle from here to the specific constraint in the document outline view.Pellucid
S
1

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.

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

Stig answered 17/2, 2015 at 6:57 Comment(0)
I
1

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).

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

Insure answered 29/4, 2016 at 23:12 Comment(0)
O
0

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.

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

Ornamented answered 1/3, 2014 at 4:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.