6

I have a View controller, in which the view has two image views and two text views. I turned off auto layout, and I programmatically set the distance between the first text view and the first image view by using this code: The following code is in the viewDidLoad method of my custom view controller class. I have set the autoresizing mask to no in both cases, so I have no idea why the code doesn't work. (tf2_logo is the image view and itemName is the text view)

self.tf2_logo.translatesAutoresizingMaskIntoConstraints = NO;
[self.backpackBackground addConstraint:[NSLayoutConstraint constraintWithItem:self.itemName attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.tf2_logo attribute:NSLayoutAttributeTop multiplier:1.0 constant:-1.0]];
[self.backpackBackground addConstraint:[NSLayoutConstraint constraintWithItem:self.tf2_logo attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.backpackBackground attribute:NSLayoutAttributeLeft multiplier:1.0 constant:17]];

Now I want to do the same thing with my other text view, basically I wanted to keep the distance between the itemName text view and the text view at a certain distance. I used this code: (tf2 is my other text view)

self.tf2.translatesAutoresizingMaskIntoConstraints = NO;
[self.backpackBackground addConstraint:[NSLayoutConstraint constraintWithItem:self.itemName attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.tf2 attribute:NSLayoutAttributeTop multiplier:1.0 constant:-3.0]];
[self.backpackBackground addConstraint:[NSLayoutConstraint constraintWithItem:self.tf2 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.tf2_logo attribute:NSLayoutAttributeRight multiplier:1.0 constant:20]];

After implementing this code, the tf2 text view doesn't even show up in the view controller. What is the problem?

EDIT: You can download the whole project here: https://www.dropbox.com/sh/u820u2ndyrncuz8/P4atI-9CAx

Vishwa Iyer
  • 841
  • 5
  • 14
  • 33

2 Answers2

3

EDIT#2: You mentioned that you turned off auto layout, because UITextView has that little gap on top in iOS7. To remove the gap, try this:

self.tf1.textContainerInset = UIEdgeInsetsZero;

When you log the original value of the textContainerInset it shows: {8, 0, 8, 0} . The two 8's are responsible for the gap (one at the top). The line above sets all values to zero and the content is nicely aligned to the top of the frame.

(EDIT#1: Completely changed the answer)

I assume you primarily want to have a flexible height of the imageName UITextView. First I suggest to use auto layout. You can set constraints in Xcode according to the following image:

Constraints

The red lines are the constraints. The green line is special: It shall be a height constraint and you create an outlet for it in the view controller. (Open the document outline view, locate the height constraint in the tree and control-drag it to the code.)

Then in the viewDidLoad method:

CGSize size = [self.tf1 sizeThatFits:self.tf1.frame.size];
self.tf1Height.constant = size.height;

The height of the "lore ipsum" field now adjusts to its content.

Screenshot

Rainer Schwarze
  • 4,725
  • 1
  • 27
  • 49
  • You can download the project here: https://www.dropbox.com/sh/u820u2ndyrncuz8/P4atI-9CAx – Vishwa Iyer May 04 '14 at 02:33
  • I didn't want to use autolayout because for the UITextView under the large UIImageView, I wanted the text to start at the top of the textview, and not the middle as xcode normally puts it. I looked up how to do it on Stackoverflow, and the user suggested that I turn off autolayout. – Vishwa Iyer May 04 '14 at 11:14
  • Can you give more details on the text placement problem? With my tests in a UITextView the text sits at the top (with UILabels it sits in the vertical center). – Rainer Schwarze May 04 '14 at 17:25
  • Did you turn on autolayout? – Vishwa Iyer May 04 '14 at 17:35
  • The project is using auto layout. I added constraints in the scene, so it is also auto layout aware during design. – Rainer Schwarze May 04 '14 at 20:32
  • In the meantime I added some code which shifts the tf2 and tf2_logo controls down according to the height change of the imageName field. When I ran your code I noticed, that backpackIcons.origin is NULL in my case and therefore the imageName field does not contain any visible content. Are you sure, that the data comes in as you expect it? – Rainer Schwarze May 04 '14 at 20:51
  • For the backpackIcons.origin, the data isn't coming as I expected it. I have to fix that. Could you provide a link so I can download your project with the autolayout? – Vishwa Iyer May 04 '14 at 23:30
  • I now understand what you mean regarding the strange top gap in UItextView. Take a look at this: http://stackoverflow.com/a/19706526/1396265 . At first I used auto layout in my test project only. I took a quick look now to convert your scene to auto layout. Basically it took the steps which I outlined in my answer. One extra step: The view which contains your controls must be get the constraints to its superview too. – Rainer Schwarze May 05 '14 at 20:27
  • Would replacing the UITextView with UILabel be an option for you? (Set the lines property of the label to 0 and it will adjust to multiple lines by itself...) – Rainer Schwarze May 05 '14 at 21:24
  • @VishwaIyer can you try this link: https://www.dropbox.com/s/rupzerjppehzgag/Steam%20Backpack%20Viewer%202.zip – Rainer Schwarze May 06 '14 at 20:36
-4

Have you tried using frames instead of constraints? If your not using autolayout I think frames might be easier to read/implement.

sample:

// tf2 will be placed at (0,0) in superview and have width of 100 and height of 20

tf2.frame = CGRectMake(0, 0, 100, 20); 

you can play around with different values to get your layout as desired.

MJN
  • 10,748
  • 1
  • 23
  • 32