Let us build a button
Creating buttons in Xcode is pretty simple. You go into interface builder, you select the button, you drag it over and drop it into the storyboard, and then you connect it to the ViewController as an Outlet or an IBAction. Not a lot to it right? Well, what if you need to make a button but don’t want to use the Main.Storyboard?
It’s actually pretty simple to add a button into your app by creating one and either dropping in into your UIView or adding it to a StackView. Let us take a look at how.
First, create your button. To do this, you will create a new constant and assign it as a UIButton.
Looking at the code above, you declare your constant using the “let” keyword. Then you assign it a button using the UIButton class and give it a specific type. The type specify’s a style for the button. These are pre-existing styles but if you want to run with your own style you can select “custom”.
Now that you have a button, you can change some of the attributes right in code.
Now that you have your custom button looking good, and pointing to a function to run. We need to size it. This threw me, but we can manipulate the constraints for the button inside the code. We first create the constraints for the button and then add them to an array assigned to the button.
There are loads more constraints to pick and assign, that could be a whole blog post by itself. But when you create them, you just have to make sure they all get added to the array and passed to the .addConstraints method. You could also assign them all to an array before and just pass that array into the method to assign the constraints. Up to you and how you want to write the code.
Ok, all this is well and good but how do we actually add the button? If you want to add it to the current view you can add it using something like:
Depending on how you created the button above, this would then drop the button in with the constraints assigned. You could have added in additional constraints that would place the button correctly and keep it sized appropriately.
Also, if your using a stack view and have it set as an IBOutlet to reference then you could just drop it right into the stack view.
This drops your button right into the stack view, so just double check your constraints before you throw it in there or it may knock things a little weird.
Ok, this wasn’t to deep, but I hope it helps a bit with how to create a button and a few options when dropping it in programmatically. I will do another post on constraints programmatically later and then dive deeper into the UIButton options and how to place and work with them.