Skip to content

Instantly share code, notes, and snippets.

@dezinezync
Last active November 8, 2021 05:56
Show Gist options
  • Save dezinezync/22a28be02643b87be2b3dc4b055c2836 to your computer and use it in GitHub Desktop.
Save dezinezync/22a28be02643b87be2b3dc4b055c2836 to your computer and use it in GitHub Desktop.
UIButton additions for macCatalyst
//
// UIButton+Catalyst.swift
// Elytra
//
// Created by Nikhil Nigade on 09/07/21.
// Copyright © 2021 Dezine Zync Studios. All rights reserved.
//
import UIKit
#if targetEnvironment(macCatalyst)
@available(iOS 14, macCatalyst 14, *)
extension UIButton {
private static let keyEquivalentKeyPath: String = "_visualProvider._element._button._cell.keyEquivalent"
/// Set to "\r" to mark as the primary action button in the context. See Apple's documentation for various possible values and combinations.
var keyEquivalent: String? {
get {
value(forKeyPath: UIButton.keyEquivalentKeyPath) as? String
}
set {
setValue(newValue, forKeyPath: UIButton.keyEquivalentKeyPath)
}
}
private static let hasDestructiveActionKeyPath: String = "_visualProvider._element._button.hasDestructiveAction"
/// Set to true if the button performs a destructive action. Rendered with red title.
var hasDestructiveAction: Bool {
get {
(value(forKeyPath: UIButton.hasDestructiveActionKeyPath) as? Bool) ?? false
}
set {
setValue(newValue, forKeyPath: UIButton.hasDestructiveActionKeyPath)
}
}
private static let controlSizeKeyPath: String = "_visualProvider._element._button._controlSize"
/// Refer to https://developer.apple.com/documentation/appkit/nscontrolsize for possible values. 0 is regular.
var controlSize: Int64 {
get {
(value(forKeyPath: UIButton.controlSizeKeyPath) as? Int64) ?? 0
}
set {
setValue(newValue, forKeyPath: UIButton.controlSizeKeyPath)
}
}
}
#endif
@dezinezync
Copy link
Author

MIT License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment