0

I have a problem when i will try to make a left/top/bottom margin with an UIImageView in a cell. It don't care what i will try to set as Top/Bottom/Left Space, i will always get the same result -> all Images are without any margin.

I have a fixed height and width, i will just want to add top/left margin.

enter image description here

Is there any way to force the Autolayout to add a margin here?

Thanks in advance for any tips. Edit: Thanks for you answer. Ill tried it, but its still not working. There is always no margin between cell and image:

Here are my constraints. (ill just take 8px as space to superView)

enter image description here

derdida
  • 14,784
  • 16
  • 90
  • 139
  • Getting auto layout to work on a UITableViewCell is to ensure you have constraints to pin each subview on all sides — that is, each subview should have leading, top, trailing and bottom constraints.check the link for similar type of issue http://stackoverflow.com/questions/29231390/uitableview-strange-layout-behavior-changes-on-scroll/29327082#29327082 – Ram Vadranam Apr 16 '15 at 08:48

2 Answers2

3

Actually i could't get quite to work on this issue. but i am sure that it will going to work if you try this things in your code.

Using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights

Edit:

Set the new UIImageView size and position to the values shown in the screenshot below.

enter image description here

Now select UIImageView and add following contraints.

Set leading, top and bottom to 20.
Set width and height to 100.
Make sure that Constrain to margins is not checked.
Tap the Add 5 constraints button.

enter image description here

Select the image view to show its constraints, and then select its bottom constraint to edit it. In the attributes editor, change its Relation to Greater Than or Equal and its Priority to 999.

enter image description here

Next, select the image view to show its constraints and then select its height constraint. In the attributes editor, change its Priority to 999.
Likewise, select the image view’s width constraint and change its Priority to 999.

To set following constraints, you will get you answer.

Community
  • 1
  • 1
Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
  • Thank you, but its not working. Ill edited my start post – derdida Apr 16 '15 at 10:15
  • @derdida, are you sure you set the proper constraints that i was shown in post. because here is working fine. – Jatin Patel - JP Apr 16 '15 at 10:40
  • @derdida, can you please check that your `UIImageView` mode is `Aspect Fit`. – Jatin Patel - JP Apr 16 '15 at 10:48
  • Thanks - already checked and changed it to aspect fit - and the same problem. Can u maybe upload (github?) your projekt, so i can take a look at it? – derdida Apr 16 '15 at 10:54
  • All Constraints should be fine, i am using Autolayout multiple times, and normally it works fine - could this be a problem because i am doing an universal app? There are a few (greyed out) constraints in my view. – derdida Apr 16 '15 at 10:56
  • @derdida Go to `Document Outline` remove all conflict constraints. – Yuyutsu Apr 16 '15 at 11:01
  • There are no conflict constraints. They are all blue and are looking fine - but there is still no space between image and cell. – derdida Apr 16 '15 at 11:02
  • @derdida do you want to add separate between two images, Am I right? – Yuyutsu Apr 16 '15 at 11:04
  • Hmm totally stupid, but: ill deleted my UITableViewCell, created everything new - and now it works. Thanks! – derdida Apr 16 '15 at 12:52
1

Add this code in cellForRowAtIndexPath


 UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectZero];/// change size as you need.
    CGRect frame = separatorLineView.frame;
    frame.size.height = 5.0;
    frame.size.width = tableView.frame.size.width;
    separatorLineView.frame = frame;
    separatorLineView.backgroundColor = [UIColor redColor];

    [cell.contentView addSubview:separatorLineView];
    return cell;

OR
Follow these links Autolayout or Autolayout programatically

Now use these constraints

storyboard 1st image

simulator
2nd image

This might helps you :)

Yuyutsu
  • 2,509
  • 22
  • 38