By johnnyspo

2014-02-28 04:13:45 8 Comments

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.


@Giuseppe Barbalinardo 2016-04-29 23:12:41

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.

@David 2015-02-17 06:57:27

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.

@matt 2014-03-01 04:24:20

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

@johnnyspo 2014-03-02 22:55:52

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!

@Andrew Paul Simmons 2015-11-19 23:53:19

Thanks matt!! You just saved me so much time!!

@KoreanXcodeWorker 2016-04-19 09:15:47

So strange that cannot connect from File's Owner to IB except margin constraints, but this answer works well.

@johnnyspo 2014-03-01 04:02:27

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.

@motox 2014-02-28 04:24:18

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.

@ryan cumley 2014-02-28 04:30:04

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 2014-02-28 04:45:07

Declaring in the @interface section of .h or .m file doesn't matter.

@johnnyspo 2014-02-28 11:37:15

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.

@motox 2014-03-03 04:50:12

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

Related Questions

Sponsored Content

94 Answered Questions

30 Answered Questions

[SOLVED] UIScrollView Scrollable Content Size Ambiguity

32 Answered Questions

[SOLVED] Loaded nib but the 'view' outlet was not set

42 Answered Questions

[SOLVED] How to check for an active Internet connection on iOS or macOS?

13 Answered Questions

[SOLVED] How do I animate constraint changes?

1 Answered Questions

[SOLVED] Getting iOS outlets wired in InterfaceBuilder

4 Answered Questions

[SOLVED] Add spacing constraint to superview in Xcode 5

1 Answered Questions

How do I connect a web view in the storyboard to an outlet in Xcode 5?

Sponsored Content