A SwiftUI Package to easily access a view's size and inset information.
Rid your code of ugly GeometryReaders.
import SwiftUI
import EasyGeometryProxy
struct ContentView: View {
var body: some View {
NavigationView {
// ...
}
.withViewSize(onAppear: someFunction)
}
}
EasyGeometryProxy
is a Swift extension for SwiftUI's View
type.
It offers a cleaner replacement to the traditional use of GeometryReader
on a view's background, providing an elegant and straightforward way to access a view's size and safe area insets.
-
Access Size:
withViewSize(onAppear:onChange:)
-
Access Safe Area Insets:
withViewInsets(onAppear:onChange:)
-
Access Both Size and Insets:
withViewGeometryProxy(onAppear:onChangeSize:onChangeInsets:)
All functions are overloaded to support various parameter combinations, allowing you to access the data you need, when you need it. All onChange
variants can be adjusted as follows:
- Omitted entirely.
- Defined with 0 parameters.
- Defined with 2 parameters.
EasyGeometryProxy utilizes iOS 17's new onChange(of:initial:_:)
function.
Working with your view's size has never been so clean.
1. Accessing Safe Area Insets on Appearance
Before:
.background {
GeometryReader { proxy in
Color.clear
.onAppear {
setBottomSafeArea(proxy.safeAreaInsets)
}
}
}
After:
.withViewInsets(onAppear: setBottomSafeArea)
2. Accessing Both Size and Insets on Appearance and Change
Before:
.background {
GeometryReader { proxy in
Color.clear
.onAppear {
someFunction(proxy)
}
.onChange(of: proxy.size, perform: someFunction)
.onChange(of: proxy.safeAreaInsets, perform: someFunction)
}
}
After:
.withViewGeometryProxy(
onAppear: someFunction,
onChangeSize: someFunction,
onChangeInsets: someFunction
)
3. Demonstrating Overloading with 0 or 2 Parameters
Explicit
.withViewGeometryProxy { proxy in
someFunction(proxy)
} onChangeSize: {
/* 0 parameters */
someFunction()
} onChangeInsets: { oldInsets, newInsets in
/* 2 parameters */
someFunction(oldInsets, newInsets)
}
Implicit
.withViewGeometryProxy(
onAppear: someFunction,
onChangeSize: someFunction, // 0 parameters
onChangeInsets: someFunction // 2 parameters
)
-
To use
EasyGeometryProxy
, in Xcode, navigate to:File ➡ Add Package Dependencies
-
In the top-right search bar, enter:
https://github.com/DevonMartin/EasyGeometryProxy
-
Ensure your project is selected in the drop down
Add to Project
menu, and then clickAdd Package
.
That's it! Then, in any file you want to access the package, simply import EasyGeometryProxy
.
Copyright © 2023 Devon Martin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.