Giving UIView rounded corners

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Giving UIView rounded corners

Giving UIView Rounded Corners: The Easy Way πŸŽ‰

So you have a login view that contains a subview with a UIActivityView and a UILabel saying "Signing In…". But you're facing an issue – those corners aren't rounded. πŸ˜• Fear not, we've got you covered! In this blog post, we'll explore different solutions to help you achieve those perfectly rounded corners. Let's get started! πŸš€

Option 1: Do it in Interface Builder 🎨

If you prefer working with Interface Builder, there's good news – you can round those corners directly in your xib file!

  1. Select the subview you want to round the corners of.

  2. Go to the Attributes inspector.

  3. In the User Defined Runtime Attributes section, click on the "+" button.

  4. Set the Key Path to layer.cornerRadius.

  5. Set the Type to Number.

  6. Set the Value to the desired corner radius. For example, if you want a perfectly rounded corner, set it to view.bounds.height / 2. Feel free to experiment with different values until you get the desired result.

πŸ’‘ Pro Tip: Use the same corner radius for both the UIActivityView and the UILabel to maintain consistency.

Option 2: Programmatically Round Those Corners πŸ“

If you prefer to handle this programmatically, you can do so too! Here's a step-by-step guide:

  1. Import QuartzCore at the top of your file: import QuartzCore.

  2. Select the subview you want to round the corners of.

  3. Add the following line of code to the relevant part of your codebase:

    view.layer.cornerRadius = view.bounds.height / 2 view.layer.masksToBounds = true

    This sets the corner radius to half the height of the view, giving you a perfectly rounded corner.

Dealing with Common Issues πŸ› οΈ

Issue: CornerRadius Has No Effect

This issue commonly occurs when the clipsToBounds property of the parent view is set to false. When this happens, the parent view doesn't clip its subviews, meaning that corner rounding will not be visible.

Solution: Ensure the clipsToBounds property of the parent view is set to true:

parentView.clipsToBounds = true

Issue: Corners Lose Their Roundness on Device Rotation

Sometimes, rounded corners may lose their shape when the device rotates. This occurs when the parent view's frame is not correctly updated during rotation.

Solution: In your view controller, override the viewDidLayoutSubviews() method and update the corner radius:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    view.layer.cornerRadius = view.bounds.height / 2
}

Take It to the Next Level! πŸ’₯

Now that you know how to give your UIView rounded corners, it's time to take your app to the next level! Experiment with different corner radii, colors, and shadow effects to add a unique touch of personality.

Have any other UI questions or ideas? Share them with us in the comments section below! We'd love to hear from you. πŸ‘‡

Happy coding! πŸ’»βœ¨

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

πŸ”₯ πŸ’» πŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! πŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings πŸ’₯βœ‚οΈ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide πŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? πŸ€” Well, my