I am working on a macOS app where I want to give users the ability to switch between light and dark mode.
For iOS apps, this can be done by simply overriding the UserInterfaceStyle of UIWindow. Like so:
window.overrideUserInterfaceStyle = .dark //.light
The problem: NSWindow doesn't have a UserInterfaceStyle property.
I have tried to set the NSAppearance
window.appearance = NSAppearance(appearanceNamed: NSAppearance.Name.aqua, bundle: nil)
without success. It returns me an error saying: "RunTimeThemeRefForBundleIdentifierAndName() couldn't find NSAppearanceNameAqua.car in bundle with identifier: ..."
I am stuck. Do you have any ideas?
Here would be a solution for iOS
Thanks!
I was on the right track!
window.appearance = NSAppearance(named: .aqua)
worked.
There is actually a guide to set the appearance of macOS apps from apple.
Note: This also works if you have all your content inside a NSPopover, since it too has a property appearance.
Related
Code for the thing
let fileBrowser = UIDocumentPickerViewController(documentTypes: listOfAllTypesOfFiles, in: UIDocumentPickerMode.open)
fileBrowser.allowsMultipleSelection = false
fileBrowser.delegate = self
navigationController?.present(fileBrowser, animated: true, completion: nil)
Not sure why, but it shows that empty space at the bottom. Anyone knows what that is?
Found the issue. It was customizing tab bar via appearance. In particular that big chunk of view was some messed up version of shadow image which looks fine in main app, but not so much there. Don't customize your stuff like that people :)
It turns out that I had a global appearance() customisation causing issues with the document picker.
In my case this was the line causing the issue, removing it enabled the UIDocumentPickerViewController to display correctly.
UITabBar.appearance().isTranslucent = false
I've applied the appearance to my UITabBar directly instead of globally.
Alexander above (although downvoted) helped track the solution. Hope this helps whoever comes after.
It seems that Xcode 12 is no longer supporting a default user interface style provided in the info.plist for iOS14. Is this a bug with the beta or is there any other way to provide this information without the need of overwriting every single element?
The following code did the trick
UIApplication.shared.windows.forEach { window in
window.overrideUserInterfaceStyle = .light
}
I am just about to port my UIKit app to iOS 13 and got stuck when trying to change the text color in my search bar to look nice in dark mode. I have tried already all proposed solutions and code snippets here on stackoverflow.
Is it a bug in iOS 13 that changing the search bar text color (even by using the new attribute searchTextField) does not work yet? Or does anybody have a working example for iOS 13 to share?
Thank you very much!
I solved the problem by myself - sorry, was my fault :-)
I had the following line of code in AppDelegate which I had overseen:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = convertToNSAttributedStringKeyDictionary([NSAttributedString.Key.foregroundColor.rawValue: UIColor.black])
After changing the color to UIColor.label it works fine in light mode and dark mode :-)
How do I change the size and colour of the flashing cursor in my textfield using my storyboard for my Mac app.
I assume I have to connect it into the view controller script (control drag) and edit some uitextfield color parameter for the cursor?
But I don't seem to be getting anywhere fast. I am working in swift 2.2 with Mac app Storyboards. Like Ulysses and Taskpaper.
In Swift 3:
Change YOURTEXTFIELD to your textfield and you are set:
(YOURTEXTFIELD.value(forKey: "textInputTraits") as AnyObject).setValue(UIColor.black, forKey: "insertionPointColor")
Documentation: https://developer.apple.com/documentation/appkit/nstextview/1449309-insertionpointcolor
Search for _drawInsertionPointInRect that's the private method that you need. Though you also need some other stuff I think... but it's all mixed into my code so I can't say for sure what it all is. Anyway search for _drawInsertionPointInRect and you'll get to some explanations.
I'm using custom button (label button). Works fine on SDK 5.0 but when I simulate to SDK 4.3 it gives me bigger font. Does anyone know what would be the issue is?
Yes, fonts have changed beetween SDK4.3 and SDK5. SDK4.3 default fonts where bolder/bigger that the one set by default in SDK5. What is the reason of this... I don't know. You should set yourself the wanted font instead of using the default system one to have a similar rendering in both case.