How to make a simple rounded button in Storyboard?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Creating a rounded button in Storyboard is an essential skill for iOS developers to enhance the appearance and user experience of an application. Storyboard is part of Apple's Xcode IDE, featuring a drag-and-drop interface for designing user interfaces visually. In this article, we'll guide you through the process of creating a simple rounded button using Storyboard, complete with technical explanations and examples.
Create a UIButton
First, you'll need to create a `UIButton` in your Storyboard. Here are the steps to do this:
- Open Xcode and create a new or open an existing iOS project.
- Navigate to the Storyboard file, usually named `Main.storyboard`.
- Drag a UIButton from the Object Library into your view controller on the Storyboard.
Customize the UIButton
To make the button rounded, you'll need to adjust its `layer` property, specifically the `cornerRadius`. Here's how you can do this:
Step-by-Step Instructions
- Select the UIButton on your Storyboard.
- Open the Identity Inspector (on the right sidebar) and make sure that your button class is set to `UIButton`.
- Switch to the Attributes Inspector and change the button's properties like title, background color, etc., as desired.
- Open the Assistant Editor by clicking the overlapping circles icon at the top right corner. This will allow you to see both your Storyboard and related ViewController code.
- Control-drag from your button to the ViewController code to create an IBOutlet. Name it `roundedButton`. This connection allows interfacing between the Storyboard and the code.
Implement Rounded Corners in Code
You'll now add the code to make the button's corners rounded:
- `layer.cornerRadius`: The `layer` property of a `UIView` provides access to adjust its appearance. Setting the `cornerRadius` property modifies the radius of the button's corners.
- `clipsToBounds`: Set to `true` to ensure any subview content does not exceed the bounds of the button.
- Border: Add a border to the button using `roundedButton.layer.borderWidth` and `roundedButton.layer.borderColor`.
- Shadow: Create a shadow using properties like `roundedButton.layer.shadowColor`, `roundedButton.layer.shadowOpacity`, and `roundedButton.layer.shadowOffset`.
Related reading
- How to make a smooth image rotation in Android?
- how to make a specific text on TextView BOLD
- How to make a transparent UIWebView
- How to make a UILabel clickable?
- How to make a UILabel clickable?
- How to make an alert dialog fill 90 of screen size?
- How to make an Android device vibrate? with different frequency?
- How to make an Android device vibrate? with different frequency?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.