1

enter image description heremy collection view cell is full size..i have collection view cell and it contains button..i successfully display all button with it's title in collection view cell. but i am having one issue that some button title is too large..i want to change it's widht based on text...how to set button width based on text..i have vertical collection view

my array is

var arr = ["butto1 butto1 butto1","butto1 butto1butto1butto1","butto1","butto1 butto1butto1butto1"]

I have searched so many time but still i haven't point out ...any on help me..Thank you

Foolish
  • 159
  • 10

1 Answers1

1

You haven't given much information that would help in diagnosing your problem.

The button should automatically resize to fit the text unless you have set constraints that stop it from growing.

If you have set a left and a right constraint to equal a constant then it will not be able to grow.

As far as I can tell, you have two options.

  1. Only set 3 out of 4 constraints. For example the following image has 3 constraints set which allow the button to grow downwards.

Image showing three out of four constraints set on UIButton

  1. Set the right constraint to "Greater than or equal to" instead of "equal" in the interface builder. This will allow the button to grow to a maximum size which is specified by the constraints.

Image representing the greater than or equal to constraint setting

Also make sure to set your button to word wrap so that text continues on the next line.

Vlad
  • 932
  • 6
  • 18
  • Thank you for response..but i don't want to new line for button text..i just want to change button width based on text..i found one solution but it does not work. see [link which i refer](https://stackoverflow.com/questions/23134986/dynamic-cell-width-of-uicollectionview-depending-on-label-width) – Foolish Jun 07 '17 at 15:46
  • @Foolish Button size should automatically accomodate the text. Make sure your Line Breaks are not set to "Clip" in the attribute inspector. Can you update your question with the constraints that are set on the button? – Vlad Jun 07 '17 at 15:53
  • see my updated question my collection view cell is full size – Foolish Jun 07 '17 at 16:07
  • @Foolish I've attempted to replicate the problem, however I have been unsuccessful using exactly the same constraints as you. It works fine on my end. I'm not sure what else could be the issue. – Vlad Jun 07 '17 at 16:32