How to make a simple rounded button in Storyboard?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.