1

UPDATE: I am adding a photo that may better demonstrate the type of thing I am trying to achieve. enter image description here I am creating an app where a user can set as many filters they like on a stream of images. I would like to display each filter they have set as a button with an X in it if they would like to remove it. I have created my own buttons and assigned actions to them, but this would be my first dynamic button project.

My confusion how to programmatically create the button, assign it an action that knows which filter to act on, and then arrange it so that the buttons behave like text in a text box such that if a button can't fit on the screen it "word wraps" to the next line.

I have a an array of the filters they have applied that I can loop through, I just don't know what the mechanism is for arranging them so that each button acts like its own word in a sentence. As a work around I could use a table/collection view but since the width of the filters varies amongst the potential buttons I was really hoping to not force a uniform width as those would require. I have seen apps that do this so I am hoping the experts on here can help.

I suspect the solution looks something like:

  • For loop that has for each filter in Filters
  • Create and configure a UIButton with the text title of the filter
  • Assign an action that takes an argument so I know which filter to remove
  • Assign constraints appropriately so it fills the width of the screen but "word wraps" once it can't fit and whatever container object it is in increases in height which would need to adjust in landscape mode

    // Can a #selector take an argument like the filter's array location? myButton.addTarget(self,action: #selector(helloButton), forControlEvents: .TouchUpInside )

Stack View vs Manual Layout Appearance

Daniel Patriarca
  • 361
  • 3
  • 20
  • Like a `UICollectionView`? – Moshe Gottlieb May 12 '19 at 21:35
  • Please use UICollectionView with the label with a word wrap property and add cancel button at trailing of UILabel. – Mitesh Mistri May 13 '19 at 11:20
  • The buttons will all have variable width's based on the title which is why I was hoping to not have the uniform collection/table views. – Daniel Patriarca May 13 '19 at 17:54
  • Every item in a collection view can have a unique size. – rob mayoff May 13 '19 at 18:08
  • Is collection view the way to do something like image I posted? If so is there a link or search terms you can provide for how I would configure a collection view to look similar? I haven't come across anything for letting a collection view cell size itself based on a button's size that is in it. – Daniel Patriarca May 13 '19 at 18:24
  • Adding collection view to my search turned up this link: https://codentrick.com/create-a-tag-flow-layout-with-uicollectionview/ . Is that the approach you are recomending? If you have any better links or tutorials on the topic that would be very helpful, thanks. – Daniel Patriarca May 13 '19 at 18:29

0 Answers0