


Xcode)Īdditionally, the app can read any HEX or RGB values that you might type and show you the corresponding color and code.
#Mac color picker code#
Drag and drop your code from the application to any program you are working on. With the new Menubar option, you can quickly access your recently copied or recently saved colors. You can copy the code with a single click, drag 'n drop or use the new shortcuts to paste it into your source code. Then a preview of the color, its Hexadecimal, RGB value, NSColor and UIColor is displayed. With Color Picker, you can choose any color from everywhere on your screen using the magnifier and selecting your desired pixel. An efficient color picker, for professionals, who care about optimizing their workflow and speed up their process. However, this only works if you ensure the UIColorPickerViewController is kept around.What does Color Picker do? Color Picker, the utility application, designed for all Cocoa developers and application designers. The idea is that the color can be tweaked further using this window.
#Mac color picker mac#
However there’s a gotcha: The Show Colors… button dismisses the picker and shows the default Mac color picker instead. The Show Colors… button shows the default macOS color picker ( NSColorPanel): However, when the picker is presented directly on top of content, it looks good and fits into the Mac: This is puzzling and will cause compatibility issues, as the color picker works completely different, and it doesn’t look great at all: Xcode’s view debugger doesn’t display hosted AppKit views inside the UIView hierarchy, but we can use LLDB to dig into the hierarchy:

The Mac version uses NSColorPickerMatrixView, an AppKit view, which is bridged to UIKit via _UINSView. You can test this by enabling Show Designed for iPad in Xcode and selecting the new Mac target. But most surprising is that even in the new iOS emulation mode (Apple Silicon only), it uses the AppKit look. In a surprising decision, Apple shows a completely different color picker when your app runs on the Mac, and it doesn’t matter if the app runs via Catalyst (Scale Interface to Match iPad) or Catalyst (Optimize Interface for Mac). These issues have been reported under FB8980868. There can be a short flickering as the remote plugin is loaded, but it’s usable. Using this setup, the color picker can be pushed on a navigation controller stack.

However, we can mitigate this somewhat if we embed UIColorPickerViewController into a custom container: The color picker is hosted as a remote view controller, which might explain some of these problems: Remote view controllers in UIKit are finicky and often have interesting bugs. The background color is missing, and there’s a weird animation related to safeAreaInsets. It looks like Apple hasn’t tested this use case. If we try to instead push the picker onto a navigation controller, the default behavior is pretty bad: While this isn’t documented, Apple designed the color picker to be presented modally, and everything works as expected. Therefore, I question the usefulness of this delegate. While Apple also offers a colorPickerViewControllerDidFinish delegate call, this method isn’t called when the picker is presented as a popover and dismissed by tapping outside the view - the call is only called when dismissing the control via the Done button when the picker is presented modally. Using Combine’s KVO wrapper is an extremely elegant way to receive color changes: You can use the delegate pattern to be notified about selectedColor property changes, or you can use KVO. Using UIColorPickerViewController in UIKitĪpple’s UIColorPickerViewController has a compact API and is straightforward to use. I’ll skip UIColorWell, as it behaves almost exactly like its SwiftUI counterpart, and I’ll instead focus on UIColorPickerViewController. On small form factors, the picker is presented modally and adapts well to landscape mode.įor more advanced use cases, we need to look at the UIKit API. On iPad, the picker is displayed as a popover. The color picker looks just like the one we’re used to on macOS, and its behavior is the same on both SwiftUI Mac and SwiftUI Catalyst. The control can bind to either Color or CGColor, and there’s an option for supportsOpacity that defaults to true.
