Programatically adding buttons


I’m programatically building my tableView height so that it sits in the bottom half of my view controller. Using this:

tableView.frame = CGRect(x: 0, y: (self.view.bounds.height / 2) - 100, width: self.view.bounds.width, height: self.view.bounds.height / 2)

I’ve just tried to add to buttons on my storyboard that sit on top of the table and are both 50% of the width. I pinned each button’s bottom edge to the top of the tableview and to the side of the screen, I then dragged from button 1 to button 2 and did equal widths. Which I think is wrong, as when my screen loaded the buttons weren’t 50% of the width and the buttons were right in the top corners of the screen and no tableview was visibile.

What is the correct/best way to align too buttons, so they become 50% of the width regardless of what device they’re on? :slight_smile:

@jonny. Thanks very much for your question! I personally would recommend the “selector” UI element which essentially joins multiple buttons together, and forces the user to make just one selection. Each selection functions exactly like a button, and what I would do is essentially make the width of the entire selector UI element to be identical to the UITableView, and this should solve your problem. Each option from the selector should be linked to whatever method your buttons are currently pointing to, so that should be a trivial fix. :slight_smile:

I hope this helps!

All the best!

1 Like