-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
811b0b4
commit 5fffc17
Showing
1 changed file
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,108 @@ | ||
# KeyWindow | ||
|
||
A description of this package. | ||
Convenience functions for magically accessing the key window's safe area and frame from a SwiftUI View | ||
|
||
## Usage | ||
|
||
```swift | ||
struct MyView: View { | ||
let phi: CGFloat = 0.61803398875 /// Golden Ratio: https://www.google.com/search?q=golden+ratio | ||
ScrollView([.horizontal, .verticle]) { | ||
VStack { | ||
Text("Hello, world!").foregroundColor(.white) | ||
} | ||
.frame(width: keyWindowSafeAreaFrame.width * phi, height: keyWindowSafeAreaFrame.height * phi) | ||
.background(Color.orange) | ||
.cornerRadius(10) | ||
} | ||
} | ||
``` | ||
|
||
## Discussion | ||
|
||
The KeyWindow library is primarily driven by this UIView extension: | ||
|
||
```swift | ||
public extension UIView { | ||
static func keyWindow() -> UIWindow? { | ||
return UIApplication.shared.connectedScenes | ||
.filter({$0.activationState == .foregroundActive}) | ||
.map({$0 as? UIWindowScene}) | ||
.compactMap({$0}) | ||
.first?.windows | ||
.filter({$0.isKeyWindow}).first | ||
} | ||
} | ||
``` | ||
|
||
That is then accessed via a `View` extension: | ||
|
||
```swift | ||
public extension View { | ||
|
||
var keyWindowSafeAreaFrame: CGRect { | ||
let safeAreaInsets = UIView.keyWindow()?.safeAreaInsets ?? .zero | ||
let frame = keyWindowFrame.inset(by: safeAreaInsets) | ||
return frame | ||
} | ||
|
||
var keyWindowTopSafeArea: CGFloat { | ||
let safeAreaInsets = UIView.keyWindow()?.safeAreaInsets ?? .zero | ||
return safeAreaInsets.top | ||
} | ||
|
||
var keyWindowBottomSafeArea: CGFloat { | ||
let safeAreaInsets = UIView.keyWindow()?.safeAreaInsets ?? .zero | ||
return safeAreaInsets.bottom | ||
} | ||
|
||
var keyWindowFrame: CGRect { | ||
UIScreen.main.bounds | ||
} | ||
} | ||
``` | ||
|
||
And that's literally all of the code in the repository! | ||
|
||
## Installation | ||
|
||
### Swift Package Manager | ||
|
||
Search for `https://github.com/briannadoubt/KeyWindow` in `Xcode -> File -> Add Packages...` or add the following to the dependancies your `Package.swift` file: | ||
|
||
```swift | ||
// swift-tools-version:5.5 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "MySwiftPackage", | ||
packages: [ | ||
.package( | ||
name: "KeyWindow", | ||
url: "https://github.com/briannadoubt/KeyWindow.git", | ||
.upToNextMajor(from: "0.1.0") | ||
) | ||
], | ||
targets: [ | ||
.target( | ||
name: "MySwiftPackage", | ||
dependencies: [ | ||
.product( | ||
name: "KeyWindow", | ||
package: "KeyWindow" | ||
) | ||
] | ||
), | ||
.testTarget( | ||
name: "MySwiftPackageTests", | ||
dependencies: ["MySwiftPackage"] | ||
) | ||
] | ||
) | ||
``` | ||
|
||
### Manually | ||
|
||
Drag and drop `KeyWindow.swift` into your project and include it in your target build scheme. | ||
|