6.2. Add Expense Screen
The screen would be something like this:

We have:
Two TextFields
One Button to choose the type of expense
One Button (use the Camera icon as the background) to pick the image for receipt.
FInally, the Add Expense Button.
AddExpenseView
Let's open the AddExpenseView.swift file and update the code. Remove the PickerView and add two buttons (to select expense type and pick receipt image).
Let's see the updated code in the following:
Setting up the Buttons
buttonSelectType:
Let's look into the setup method for buttonSelectType:
It looks pretty straight forward now. At some point, we need to tweak the setup a little.
buttonTakePhoto:
Let's look into the setup method:
This button is a little different. We set a background to it, which shows a camera icon instead of text. So, we set an empty title to it. Then set an image of the system name, "camera.fill". (I found the name using the SF Symbols app). These couple of lines are very important.
For example, buttonTakePhoto.contentHorizontalAlignment = .fill means that the contents of this button can fill the whole width of the button. In our case, the content is the camera image inside the button. The same idea works with the next line: buttonTakePhoto.contentVerticalAlignment = .fill to allow the image of the button fill the height of the button.
Then, we set the frame of the image so that the image can be loaded with the content mode scaleAspectFit. (buttonTakePhoto.imageView?.contentMode = .scaleAspectFit).
The Constraints
The constraints here are pretty straightforward, except for the buttonTakePhoto. Let's look into the constraints for the Button:
We set the height and width of buttonTakePhoto. When you are working with images, you should set a frame size. The best way to set the frame size is to do it using the constraints. Use widthAnchor as the width and heightAnchor as the height of the frame.
Last updated
Was this helpful?