Change UIButton BorderColor in Storyboard
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In iOS development, customizing UI components is a crucial aspect of building aesthetically pleasing and intuitive applications. One essential customization task involves changing the border color of a UIButton
. While this is fairly straightforward in code, achieving it directly through the storyboard requires understanding a few tricks and leveraging the User Defined Runtime Attributes
feature in Xcode. This article will guide you through changing a UIButton's border color in a storyboard, supplemented by technical explanations and examples.
Customizing UIButton BorderColor in Storyboard
Xcode's Interface Builder, known as Storyboard, offers a visual way of designing UI. By default, properties such as border color are not directly changeable through the storyboard's Attributes Inspector. Instead, you can employ Xcode's User Defined Runtime Attributes
in combination with CALayer
properties to modify the UIButton's border color without writing a single line of code.
Using CALayer for UIButton Border Customization
Every UIView, including UIButton, has an associated CALayer
. This layer is responsible for rendering the view and provides properties like borderColor
that you can modify.
Steps to Change UIButton BorderColor in Storyboard:
- Select the UIButton: Open your storyboard file and select the UIButton whose border color you want to change.
- Open the Identity Inspector: With your UIButton selected, navigate to the Identity Inspector (the third tab from the left in the right-hand panel).
- Add User Defined Runtime Attributes: Scroll down to the "User Defined Runtime Attributes" section and add a new attribute:
- Key Path:
layer.borderColor - Type:
Color - Value: Choose the desired color using the color picker.
- Set Border Width: Since the border color won't be visible if the border width is zero, you need to set the border width as well. Add another attribute:
- Key Path:
layer.borderWidth - Type:
Number - Value: Set a numerical value, for instance
2, to provide a visible border.
Here's a table summarizing the key points for setting UIButton
border color in Storyboard:
| Step | Action | Key Path | Type | Value |
| 1 | Open Identity Inspector | - | - | - |
| 2 | Add User Defined Runtime Attribute | layer.borderColor | ||
| Color | Desired UIColor | |||
| 3 | Add User Defined Runtime Attribute | layer.borderWidth | ||
| Number | e.g., 2 | |||
Technical Explanation
The layer.borderColor
property requires a CGColor
as its value, hence the need to use the Color
type in User Defined Runtime Attributes. This allows the Interface Builder to automatically convert the selected color into a CGColor
.
The borderWidth
property needs to be set explicitly because without a non-zero width, the border would remain invisible, regardless of the color.
Further Enhancements
While changing the border color is a useful customization, additional enhancements can be made using similar techniques:
- Corner Radius: You can round the corners of your button by adding a
layer.cornerRadiusattribute. Set the type toNumberand value to your desired corner radius. - Shadow: Shadows can be added using
layer.shadowColor,layer.shadowOpacity,layer.shadowOffset, andlayer.shadowRadius.
Example
Let's consider an example where you want to set a red border with rounded corners for a UIButton:
- Border Color: Add
layer.borderColorwith typeColorand set it to red. - Border Width: Add
layer.borderWidthwith typeNumberand set it to2. - Corner Radius: Add
layer.cornerRadiuswith typeNumberand set it to10.
Conclusion
Altering a UIButton's border color directly in the storyboard enhances your workflow by minimizing code management for simple UI tweaks. Through the use of User Defined Runtime Attributes
, developers can leverage the capabilities of CALayer
to create visually styled buttons directly within Interface Builder. This not only streamlines the design process but also keeps the design consistent with what's visually designed in Xcode.

