0

I have a lot of trouble solving this. I have the following situation. In my storyboard I have a table view and in the table view cell I have a UIButton and another UIButton next to it. The first button has text and the second button has a star image. I would like the following:

  1. I would like to have the first button to adjust its width depending on its text/title.

  2. I would like the second button's position to move along against the trailing space of the first button

I tried multiple suggestions with sizetofit(), but can't seem to get it right

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
symenize
  • 35
  • 5

1 Answers1

3

Using Autolayout you don't have to worry about resizing the button yourself. Here's a quick layout to illustrate:

Text and info button

I've added a text button and a small round button simulating your star. In the interface builder in Xcode, I give the first button constraints for:

  • Fixed height
  • Fixed space to the left side of the view
  • Fixed space to the top side of the view

enter image description here

Then my second round button gets the following constraints:

  • Fixed width and height
  • Fixed horizontal space to the text button
  • Vertical alignment centered with the text button

enter image description here

Now when I set a long title for my button in code, you'll see that they follow each other in the way you describe:

enter image description here

Terje
  • 980
  • 9
  • 15
  • thank you for your response! I still have a problem tho. When I give the buttons said constraints, the text in the first button is compressed so instead of 'contraint' you see 'co..aint' for example – symenize Nov 19 '17 at 19:38
  • 1
    This is not always going to work. You also need to change the content-hugging priority and content-compression priority. Hugging will control how closely the control hugs its contents (Set to low so it expands or high so it hugs).. For compression, it defines how a control compresses under pressure (Set to low so it compresses easily or to high so it never compresses).. In your case, you want hugging low and compression high.. OR hugging default and compression high: https://stackoverflow.com/a/23203345/1462718 – Brandon Nov 19 '17 at 19:44
  • I got it to work! thank you! I did something wrong probably the first time – symenize Nov 19 '17 at 19:47
  • @Brandon you're right and wrong. I got it to work following Terje's solution, but only when I deleted my current buttons and replaced them for new ones. Before deletion I tried using the hugging and compression but it did not work for me but possibly that's because of constraints – symenize Nov 19 '17 at 19:49
  • @Terje your solution does not work when you use title insets. Is there a way to let it work with insets? – symenize Nov 19 '17 at 19:59
  • @symenize; No.. I'm not wrong. You happened to get it working with his code by chance. If there is anything with lower hugging and higher compression resistance, his code will not work. You had to delete your buttons possibly because that was already the case. – Brandon Nov 19 '17 at 22:31
  • how would you do it then because even though terje's solution worked, it didn't work when using insets? – symenize Nov 20 '17 at 18:30