Unable to make outlet connection to a constraint in IB

22,005

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
22,005

Related videos on Youtube

johnnyspo
Author by

johnnyspo

Updated on April 30, 2020

Comments

  • johnnyspo
    johnnyspo almost 4 years

    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.

  • ryan cumley
    ryan cumley about 10 years
    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.
  • motox
    motox about 10 years
    Declaring in the @interface section of .h or .m file doesn't matter.
  • johnnyspo
    johnnyspo about 10 years
    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.
  • johnnyspo
    johnnyspo about 10 years
    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!
  • motox
    motox about 10 years
    @johnnyspo 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.
  • Andrew Paul Simmons
    Andrew Paul Simmons over 8 years
    Thanks matt!! You just saved me so much time!!
  • KoreanXcodeWorker
    KoreanXcodeWorker about 8 years
    So strange that cannot connect from File's Owner to IB except margin constraints, but this answer works well.