The course is part of this learning path
This course focuses on iOS special features including face recognition, custom keyboards, sticker packs, and more.
This course is designed for anyone who wants to:
- Learn about iOS development and coding
- Move into a career as an iOS developer
- Master Swift skills
To get the most out of this course, you should have some basic knowledge of iOS.
Hi. Within this lecture, we're going to see how we can handle the style changes in a better way using a built-in function. Of course, there is a built-in function for that. As you can see, we can do it in some sort of way in viewDidAppear or viewDidLoad, but we have to find a more efficient way in order to do that. And let me show you how it's done. It's called trait style, traitCollectionDidChange, okay? So, once a user changes some of the traits, this function will get called. And we can do the same check under this function as well, so let me copy and paste this. So, now when user changes this, it will automatically get called. So, we would instantly change the color, the color of this button or color of whatever we want, okay? So, let me run this and test it as you can see now we are in dark mode, and we see the button in white? Now, let me go back to Settings and make it into the light mode. If we go back right now, as you can see, we see the button in blue form. Now, it's working. So, I believe this is exactly what we wanted, right? So, let me test this one more time. Let me make this into dark mode. And here we go. We have the white button. So, this is exactly the behavior that we wanted. So, you can use this traitCollectionDidChange. But maybe you're thinking that, "Yeah, do I have to do that? Do I have to bother?" Just thinking about all of this when I build an app. So, what if I don't want the users to use my app in dark mode or light mode? So, let's suppose that you built an app and in a specific View Controller, if you take this into a dark mode, it would look so bad. So, you don't want users to switch to the dark mode. Even if they switched to the dark mode, you don't want it to affect your View Controller or you don't want to affect it your whole app, okay? So, is it possible? Yes, this is possible. So, you can ignore these changes and you can actually start your app in dark mode or in light mode as always. So, how it's done? It's actually very easy. You can override the choice of the user. If you want to do that in a specific View Controller, it's very easy. You can do it with one line. But if you want to do that in the whole app then you have to go for info.plist. So, I'm just going to show you both ways. So, let me come over here to viewDidLoad because I'm just going to do that in this View Controller, and I'm just going to say override user interface style. And the style that I want is dark or light. So, maybe I don't want my app or my View Controller to be supporting the dark mode. So, if I override this interface style with light, then regardless of the user's choice, I will just show it in the light mode. As you can see I'm in the dark mode in this phone. So, my navigation bar actually is in the dark mode and this second View Controller is in the dark mode, but this View Controller is not. So, let me come over here to Settings, okay? And I just want to make this into light mode. And if I go back here, here you go. Now I'm in the light mode in the other View Controller as well, but let me try this one more time if I can. Let me come over here to dark experience, and here you go. Now I'm not in the dark mode again because I have overridden the user's choice over here. So, I'm light mode in the first View Controller and dark mode in the second View Controller. But then again, the other question may arise. So, you may think that I don't want to support dark mode in the whole app, so what do I do? Then let me comment this and then we would be certain this is working properly. Let me go into the info.plist, and I'm just going to add a new line. In order to do that, you just have to go into any line and hit the small button over here. We have done that before, right? In Travel book section for example, we asked permission from the user to get their location. So, let me add this. Let me add a new line and scroll down a little bit so that we can find user interface style. So, over here after you choose user interface style, you can come over here so you'll you go either for light like that with uppercase L or dark like that with the uppercase D, okay? So, it has to be the exactly the same, okay? Dark or light with uppercase D. And if I run this right now as you can see I'm in the dark mode. So, let's see if we were in the dark mode. Yep, we were already in the dark mode. So, let me put this into light mode. If I come over here, here we go. Now, I'm in the dark mode even though this phone is in the light mode. So, in order to be certain, I am running it from scratch and it's still in the dark mode even though my phone is in the light mode. So, here we go. Now you know how to handle these situations. So, again, you may think that I don't want to bother all of this. Okay, so my app works perfectly fine with dark mode or light mode. Do I have to override anything? No, you don't. But if you want to customize your app depending on this style choice, you're more than welcome to do so with these functions. If you don't want to support dark mode or light mode in your apps, then you're more than welcome to do so, but Apple suggests to go for both ways. So, supporting dark mode and supporting light modes. So, bear that in mind and try to understand how to handle these situations. So, let's stop here and continue within the next lecture.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.