This is a story about me spending 1 hour just to remove an UIImageView
rounded corners. I am sure most fellow developers out there will have a similar experience where you have to spend an unreasonable amount of time just to complete a simple task, and this is one of those stories.
The story that I am about to share with you does not involve any extraordinary programming knowledge, but I do like to use this story to bring out some of my views regarding the matter and the attitude that developers should have when solving problems. I hope at the end of the story, other developers, especially those who just got started can get some inspiration from it.
Here’s how the story goes…
How Hard Could This Be?
A few weeks back, I was given a task to update a legacy UI, and part of it involves removing an image view rounded corners. To give you some context, the image view is inside a collection view cell and the cell is created using the interface builder.
I am sure any experienced iOS developers out there will have the same first thought as me: “How hard could this be?”. Just look for imageView.layer.cornerRadius
within the cell implementation, remove it, then jobs done!
But wait… there is no such code found within the cell implementation! 🤨
Maybe the corner radius is being set in the view controller? Let’s check the collectionView(_:cellForItemAt:)
implementation. Unfortunately, there is still no sign of a corner radius being set.
Another possibility that I can think of is that the image view is a custom image view, but when I check the class field in interface builder, all I see is this just UIImageView
.
And now I am stuck! What else could it possibly be?
Thinking out of the Box
One thing that I have learned as a developer is always trying to think out of the box when getting stuck in a problem. I ask myself, is it possible the rounded corners that I see have nothing to do with the image view? Will it be that the source image itself has rounded corners? Let’s find out.
With that in mind, I changed the image view background color from .clear
to .red
and run the app again. If the source image itself has rounded corners, I should be able to see the image view red color corners. And once again, I am disappointed (but not surprised) that the image view does not show any red color corners.
Now that I am back to where I started, what else can I do? I can’t find the place where the corner radius is set, but maybe I can try to overwrite the image view corner radius and set it back to 0. Let’s do that in the cell’s awakeFromNib()
method and see what will happen.
This is the point where I see a ray of hope, the image view rounded corners are finally gone! 🎉
Should I Stop Here?
Now I am at a stage where I feel like I have fixed my problem, but in reality, I still have no idea what is the root cause of my problem.
Some developers might say that the problem is considered fixed, let’s move on to another task. But to me, this is just a workaround and not a true solution.
In my opinion, a workaround should only be used when we can’t find a solution, or it is not possible to implement the solution due to justifiable reasons such as time constraints or technical depth. Even with all those justifiable reasons, a workaround at its best is just a temporary solution, at the end of the day, a workaround will only increase the risk of introducing more bugs to your app.
Bearing that in mind, I decided to continue my investigation.
The Investigation Continues
Based on my previous investigation, if I am able to overwrite the image view corner radius in the cell’s awakeFromNib()
method, it is very likely that the rounded corners are caused by some unknown configurations within the interface builder.
Since I do not have any idea where to start, the best starting point will be the image view. Let’s try to replace the image view in the interface builder with a new one and see what will happen.
Surprisingly, the rounded corners are no longer there after the image view replacement!
This is a very good sign because now I can leverage the Xcode code review feature to see the differences before and after the change. Without a second thought, I hit on the ⌥⇧⌘↩ keys and this is what I see:
Eureka! That’s where the corner radius is being set!
To further verify my findings, I reverted the changes I made on the nib file and checked the image view’s user defined runtime attributes field in the interface builder.
There it is! I found the root cause of my problem!
With excitement, I removed the cornerRadius
key path. But the moment when I realized I have spent 1 hour just to remove an image view rounded corners…
My Thoughts
Always Strive for a Solution
As I mentioned earlier, a workaround is not a solution, workarounds are difficult to reason about and could easily break accidentally. Therefore, we as developers should always strive for a solution when solving problems. You wouldn’t want your fellow teammates to spend another 1 hour just to remove an image view rounded corners right?
One thing I do notice while working in the software development industry is that some developers (mostly junior developers) find it difficult to differentiate a workaround and a solution. The reason behind this is mainly lack of experience.
A workaround is always much easier to come by if compared to a solution, this is because a solution usually requires you to dig deep into the project to find out the root cause. Therefore, whenever I find a way to fix a problem, I will ask myself: Do I know the root cause of the problem? If the answer is no, then most likely the fix that I found is just a workaround.
Fixing problems with workarounds doesn’t make you a great developer. Willing to put in the effort to identify the root cause of a problem and implement the best solution is what makes a great developer. As the Roman philosopher, Seneca once said:
It is a rough road that leads to the heights of greatness.
Lucius Annaeus Seneca
When Get Stuck, Take a break
Getting stuck in a problem is very common in a developer’s day-to-day work. Different developers will take different approaches when facing this kind of situation. I personally like to get away from my computer when I am stuck with a problem, maybe go take a coffee break or take a walk in the park to get some fresh air.
When working on the same problem for a long period of time, our mind will be restricted by our own linear thinking and goes deeper and deeper into the rabbit hole. It is this kind of single pathway thinking that blocks us from discovering other possibilities and alternatives.
Taking a short break can help us to free up our own head, get creative with the problem and look at the problem from a totally different angle. I have lost count of how many times those ideas that hit during my break end up leading me to the solution.
Storyboard or Not to Storyboard
I tweeted about the incident right after I found the solution, and a lot of fellow developers responded by saying that they don’t like Storyboard and have stopped using it.
I totally agree that creating UIs programmatically is definitely a much cleaner approach compared to using interface builders. However, I am not against using interface builders either. In fact, I have used both approaches in my past projects, and each one has its own advantages and disadvantages, but that will be a story for another day.
Back to the “rounded corners” story, I would say it is not so much of a Storyboard problem but a matter of following common practices.
To me, it doesn’t matter what tools (storyboard or code) are being used, what’s important is that everyone in the team must have a mutual understanding of what should and should not be done. The incident in this article can definitely be avoided if everyone is following common practices.
Wrapping Up
I hope this short story of mine can somehow inspire you to have a positive attitude at work especially when dealing with problems. Having the correct work attitude not only keeps you passionate and motivated about what you are doing; it may also positively influence your other co-workers, thus creating a healthy team culture.
If you find it difficult to keep yourself motivated all the time, remember this:
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
John F Woods
At least that’s what works for me. 😅
If you enjoy reading this article, feel free to check out my other iOS development and Swift related articles. You can also reach out to me on Twitter, and subscribe to my monthly newsletter.
Thanks for reading. 👨🏻💻
👋🏻 Hey!
While you’re still here, why not check out some of my favorite Mac tools on Setapp? They will definitely help improve your day-to-day productivity. Additionally, doing so will also help support my work.
- ✨ Bartender: Superpower your menu bar and take full control over your menu bar items.
- ✨ CleanShot X: The best screen capture app I’ve ever used.
- ✨ PixelSnap: Measure on-screen elements with ease and precision.
- ✨ iStat Menus: Track CPU, GPU, sensors, and more, all in one convenient tool.