Skip to content

Commit 7c6aa53

Browse files
committed
Added ability to move close button to top right
1 parent 016db00 commit 7c6aa53

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,30 @@ struct ContentView: View {
6565

6666
# Customization
6767

68+
### Close Button Position
69+
70+
#### Availability: 2.2.0 or higher
71+
72+
The close button can be moved to the top right if desired. The `closeButtonTopRight` parameter accepts `bool`.
73+
74+
Example:
75+
```Swift
76+
import ImageViewer
77+
78+
struct ContentView: View {
79+
@State var showImageViewer: Bool = true
80+
@State var image = Image("example-image")
81+
82+
var body: some View {
83+
VStack {
84+
Text("Example!")
85+
}
86+
.frame(maxWidth: .infinity, maxHeight: .infinity)
87+
.overlay(ImageViewer(image: self.$image, viewerShown: self.$showImageViewer, closeButtonTopRight: true))
88+
}
89+
}
90+
```
91+
6892

6993
### Caption
7094

Sources/ImageViewer/ImageViewer.swift

+13-3
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,29 @@ public struct ImageViewer: View {
77
@Binding var image: Image
88
@Binding var imageOpt: Image?
99
@State var caption: Text?
10+
@State var closeButtonTopRight: Bool?
1011

1112
var aspectRatio: Binding<CGFloat>?
1213

1314
@State var dragOffset: CGSize = CGSize.zero
1415
@State var dragOffsetPredicted: CGSize = CGSize.zero
1516

16-
public init(image: Binding<Image>, viewerShown: Binding<Bool>, aspectRatio: Binding<CGFloat>? = nil, caption: Text? = nil) {
17+
public init(image: Binding<Image>, viewerShown: Binding<Bool>, aspectRatio: Binding<CGFloat>? = nil, caption: Text? = nil, closeButtonTopRight: Bool? = false) {
1718
_image = image
1819
_viewerShown = viewerShown
1920
_imageOpt = .constant(nil)
2021
self.aspectRatio = aspectRatio
2122
_caption = State(initialValue: caption)
23+
_closeButtonTopRight = State(initialValue: closeButtonTopRight)
2224
}
2325

24-
public init(image: Binding<Image?>, viewerShown: Binding<Bool>, aspectRatio: Binding<CGFloat>? = nil, caption: Text? = nil) {
26+
public init(image: Binding<Image?>, viewerShown: Binding<Bool>, aspectRatio: Binding<CGFloat>? = nil, caption: Text? = nil, closeButtonTopRight: Bool? = false) {
2527
_image = .constant(Image(systemName: ""))
2628
_imageOpt = image
2729
_viewerShown = viewerShown
2830
self.aspectRatio = aspectRatio
2931
_caption = State(initialValue: caption)
32+
_closeButtonTopRight = State(initialValue: closeButtonTopRight)
3033
}
3134

3235
func getImage() -> Image {
@@ -45,13 +48,20 @@ public struct ImageViewer: View {
4548
ZStack {
4649
VStack {
4750
HStack {
51+
52+
if self.closeButtonTopRight == true {
53+
Spacer()
54+
}
55+
4856
Button(action: { self.viewerShown = false }) {
4957
Image(systemName: "xmark")
5058
.foregroundColor(Color(UIColor.white))
5159
.font(.system(size: UIFontMetrics.default.scaledValue(for: 24)))
5260
}
5361

54-
Spacer()
62+
if self.closeButtonTopRight != true {
63+
Spacer()
64+
}
5565
}
5666

5767
Spacer()

Sources/ImageViewerRemote/ImageViewerRemote.swift

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public struct ImageViewerRemote: View {
1010
@State var httpHeaders: [String: String]?
1111
@State var disableCache: Bool?
1212
@State var caption: Text?
13+
@State var closeButtonTopRight: Bool?
1314

1415
var aspectRatio: Binding<CGFloat>?
1516

@@ -18,12 +19,13 @@ public struct ImageViewerRemote: View {
1819

1920
@ObservedObject var loader: ImageLoader
2021

21-
public init(imageURL: Binding<String>, viewerShown: Binding<Bool>, aspectRatio: Binding<CGFloat>? = nil, disableCache: Bool? = nil, caption: Text? = nil) {
22+
public init(imageURL: Binding<String>, viewerShown: Binding<Bool>, aspectRatio: Binding<CGFloat>? = nil, disableCache: Bool? = nil, caption: Text? = nil, closeButtonTopRight: Bool? = false) {
2223
_imageURL = imageURL
2324
_viewerShown = viewerShown
2425
_disableCache = State(initialValue: disableCache)
2526
self.aspectRatio = aspectRatio
2627
_caption = State(initialValue: caption)
28+
_closeButtonTopRight = State(initialValue: closeButtonTopRight)
2729

2830
loader = ImageLoader(url: imageURL)
2931
}
@@ -35,13 +37,21 @@ public struct ImageViewerRemote: View {
3537
ZStack {
3638
VStack {
3739
HStack {
40+
41+
if self.closeButtonTopRight == true {
42+
Spacer()
43+
}
44+
3845
Button(action: { self.viewerShown = false }) {
3946
Image(systemName: "xmark")
4047
.foregroundColor(Color(UIColor.white))
4148
.font(.system(size: UIFontMetrics.default.scaledValue(for: 24)))
4249
}
4350

44-
Spacer()
51+
52+
if self.closeButtonTopRight != true {
53+
Spacer()
54+
}
4555
}
4656

4757
Spacer()

0 commit comments

Comments
 (0)