Easier cross platform Mac and iOS development with Swift 5.1. X was abstracted out of Redacted and Whiskey.
Xcode 11.1 or higher is required.
Carthage is the recommended way to install X. Add the following to your Cartfile:
github "soffes/X"
X provides several typealias
es to make dealing with AppKit/UIKit types that are similar. Currently, X includes:
Name | UIKit | AppKit |
---|---|---|
Color |
UIColor |
NSColor |
ContentMode |
UIViewContentMode |
N/A |
EdgeInsets |
UIEdgeInsets |
NSEdgeInsets |
Font |
UIFont |
NSFont |
GestureRecognizerStateType |
UIGestureRecognizerState |
NSGestureRecognizerState |
Image |
UIImage |
NSImage |
Screen |
UIScreen |
NSScreen |
ViewType |
UIView |
NSView |
If you wanted to use a color on both platforms, you could write something like this:
let blueColor = Color(red:0.298, green:0.757, blue:0.988, alpha: 1.0)
View
inherits from ViewType
(so either UIView
or NSView
) and adds some platform specific functionality. This makes methods like layoutSubviews
work on both platforms. The UIKit API is cleaner, so NSView has methods added in View
to make it behave more like UIView
.
Here's the current list of bridged methods that work on both:
var wantsLayer: Bool // On iOS, this doesn't do anything and always returns `true`.
func didMoveToWindow() // Bridged from `viewDidMoveToWindow`
func didMoveToSuperview() // Bridged from `didMoveToSuperview`
func layoutSubviews() // Bridged from `layout`
There are several extensions for CGPoint
, CGSize
, and CGRect
that help with converting to and from strings since UIKit and AppKit have different function names. The UIKit function names are aliased on Mac. There are also initializers and computed properties you can use instead of the functions.
Enjoy.