SwiftUI — First Impressions and a Quick Tip for Changing Placeholder Text Color
When SwiftUI was first announced at WWDC 2019, I was hesitant to give it a try. I thought it was WYSIWYG and being a purest I didn’t want anything to do with it. It wasn’t until it was added to the iOS course I’m taking at Devslopes Academy that I gave it a shot.
First impression, SwiftUI is no more WYSIWYG than using storyboard in UIKit. It’s actually the best of both worlds, because what you set using the Attributes Inspector actually gets written in the code so you can see what aspects have been set and change it in the code without having to go back to the Attributes Inspector to do so. And whether you are hand coding or using the Attributes Inspector to style your code, having the canvas update automatically as changes are made is super convenient.
Right out of the gate I was impressed with how quickly the UI came together. Having used stack views in UIKit, the concept of VStacks, HStacks and ZStacks in SwiftUI was an easy transition. And unlike ScrollViews in UIKit, they work right out of the box in SwiftUI. Bonus!
Not only are building interfaces in SwiftUI fast, I feel like my designs come out looking more elegant. Adding shadows and rounding corners are a breeze and creating animations are just as easy. Don’t even get me started with tableViews; gone are the days of writing dozens of lines of code, with SwiftUI you only need a few lines of code to create a list, it’s never been easier.
While SwiftUI does amazing things to help set up your UI with ease, it is still in its infancy so it doesn’t yet handle every situation. The first issue I ran into was the inability to change the color of placeholder text in a textField. It appears this feature hasn’t been built in yet, so after scouring the internet, here is the fix I found.
Create a struct for a custom text field.
Then replace the TextField code with the following. Make sure to change the text parameter to point to your variable for that field.
For my initial assessment I would equate learning SwiftUI after using UIKit to going to another country and driving on the other side of the road, where at times your instincts will try to pull you in the wrong direction. It’s not hard, it will just take some getting used to.
Overall SwiftUI seems very well thought out and I appreciate that it’s easy to read and for the most part simple to implement. I am excited to learn all that it can do.
To delve deeper into SwiftUI, I am currently working my way through Paul Hudson’s, 100 days of SwiftUI course. I highly recommend it.
A few other great resources I stumbled upon are GoshDarnSwiftUI and Rob Sturgeon’s Medium article, The Complete SwiftUI Documentation You’ve Been Waiting For.
Happy coding!