Colorpicker control ios5/15/2023 The Color Picker is a small floating window through which you can choose colors for text, objects, lines, and other objects (depending on which application you’re in when using it, of course). However, this only works if you ensure the UIColorPickerViewController is kept around.If you’ve used a Mac for any length of time at all, you’re probably familiar with the Color Picker. The idea is that the color can be tweaked further using this window. 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.
0 Comments
Leave a Reply. |