Skip to content

Commit 4f5ee72

Browse files
authored
Merge pull request #154 from hyperoslo/fix/layout
Update layout for iPhone X
2 parents 369252e + 9e17ebb commit 4f5ee72

File tree

8 files changed

+78
-63
lines changed

8 files changed

+78
-63
lines changed

Example/DemoLightbox/DemoLightbox/ViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ class ViewController: UIViewController {
1313
button.autoresizingMask = [.flexibleTopMargin, .flexibleLeftMargin, .flexibleRightMargin, .flexibleBottomMargin]
1414

1515
return button
16-
}()
16+
}()
1717

1818
override func viewDidLoad() {
1919
super.viewDidLoad()
20-
20+
2121
view.autoresizingMask = [.flexibleTopMargin, .flexibleLeftMargin, .flexibleRightMargin, .flexibleBottomMargin]
2222
view.backgroundColor = UIColor.white
2323
view.addSubview(showButton)
@@ -30,16 +30,16 @@ class ViewController: UIViewController {
3030
LightboxImage(imageURL: URL(string: "https://cdn.arstechnica.net/2011/10/05/iphone4s_sample_apple-4e8c706-intro.jpg")!),
3131
LightboxImage(
3232
image: UIImage(named: "photo1")!,
33-
text: "Some very long lorem ipsum text. Some very long lorem ipsum text. Some very long lorem ipsum text. Some very long lorem ipsum text"
33+
text: "Photography is the science, art, application and practice of creating durable images by recording light or other electromagnetic radiation, either electronically by means of an image sensor, or chemically by means of a light-sensitive material such as photographic film"
3434
),
3535
LightboxImage(
3636
image: UIImage(named: "photo2")!,
37-
text: "🌲 🌲 🌲 🌲 🌲 🌲 🌲 🌲 🌲 🌲 🌲 🏃 🌲 🏃‍♀️ 🌲 🌲 🌲 🌲 🌲 🌲 🌲 🌲\n\nSuspendisse massa massa, maximus et finibus ac, auctor volutpat diam.\n\nPellentesque consequat magna condimentum mauris bibendum, nec ornare nisl hendrerit. Phasellus nec ultrices sem. Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n\nSuspendisse sit amet facilisis ante, ac suscipit sem. Integer feugiat sit amet erat sit amet mattis. Donec tristique, nunc ut varius elementum, nisi elit viverra ipsum, vitae aliquam justo libero in arcu. Quisque tempor et justo at malesuada. Curabitur justo dolor, ornare convallis sollicitudin sed, consectetur eu turpis. \n\nNulla et dui condimentum, laoreet lacus eu, ultrices nisl. Vivamus in ante volutpat, gravida nunc scelerisque, sagittis tellus. Nullam justo purus, sagittis a tincidunt a, maximus nec sem.",
37+
text: "Emoji 😍 (/ɪˈmoʊdʒi/; singular emoji, plural emoji or emojis;[4] from the Japanese 絵文字えもじ, pronounced [emodʑi]) are ideograms and smileys used in electronic messages and web pages. Emoji are used much like emoticons and exist in various genres, including facial expressions, common objects, places and types of weather 🌅☔️💦, and animals 🐶🐱",
3838
videoURL: URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
3939
),
4040
LightboxImage(
4141
image: UIImage(named: "photo3")!,
42-
text: "Some very long lorem ipsum text."
42+
text: "A lightbox is a translucent surface illuminated from behind, used for situations where a shape laid upon the surface needs to be seen with high contrast."
4343
)
4444
]
4545

Images/demo.png

289 KB
Loading

Images/demo2.png

297 KB
Loading

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ packed with all the features you expect:
2222
- [x] Powerful configuration.
2323
- [x] Demo project.
2424

25+
<div align="center">
26+
<img src="Images/demo.png" height="500">
27+
<img src="Images/demo2.png" height="500">
28+
</div>
29+
2530
## Table of Contents
2631

2732
* [Usage](#usage)

Source/Library/LightboxTransition.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ class LightboxTransition: UIPercentDrivenInteractiveTransition {
2828
func transition(_ show: Bool) {
2929
guard let controller = lightboxController else { return }
3030

31-
controller.headerView.transform = show
32-
? CGAffineTransform.identity
33-
: CGAffineTransform(translationX: 0, y: -200)
34-
35-
controller.footerView.transform = show
36-
? CGAffineTransform.identity
37-
: CGAffineTransform(translationX: 0, y: 250)
38-
3931
if interactive {
4032
controller.view.backgroundColor = UIColor.black.withAlphaComponent(show ? 1 : 0)
4133
} else {

Source/LightboxController.swift

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ open class LightboxController: UIViewController {
2222

2323
lazy var scrollView: UIScrollView = { [unowned self] in
2424
let scrollView = UIScrollView()
25-
scrollView.frame = self.screenBounds
2625
scrollView.isPagingEnabled = false
2726
scrollView.delegate = self
28-
scrollView.isUserInteractionEnabled = true
2927
scrollView.showsHorizontalScrollIndicator = false
3028
scrollView.decelerationRate = UIScrollViewDecelerationRateFast
3129

3230
return scrollView
33-
}()
31+
}()
3432

3533
lazy var overlayTapGestureRecognizer: UITapGestureRecognizer = { [unowned self] in
3634
let gesture = UITapGestureRecognizer()
@@ -61,14 +59,14 @@ open class LightboxController: UIViewController {
6159
view.delegate = self
6260

6361
return view
64-
}()
62+
}()
6563

6664
open fileprivate(set) lazy var footerView: FooterView = { [unowned self] in
6765
let view = FooterView()
6866
view.delegate = self
6967

7068
return view
71-
}()
69+
}()
7270

7371
open fileprivate(set) lazy var overlayView: UIView = { [unowned self] in
7472
let view = UIView(frame: CGRect.zero)
@@ -79,11 +77,7 @@ open class LightboxController: UIViewController {
7977
view.alpha = 0
8078

8179
return view
82-
}()
83-
84-
var screenBounds: CGRect {
85-
return UIApplication.shared.delegate?.window??.bounds ?? .zero
86-
}
80+
}()
8781

8882
// MARK: - Properties
8983

@@ -194,6 +188,25 @@ open class LightboxController: UIViewController {
194188
}
195189
}
196190

191+
open override func viewDidLayoutSubviews() {
192+
super.viewDidLayoutSubviews()
193+
194+
scrollView.frame = view.bounds
195+
footerView.frame = CGRect(
196+
x: 0,
197+
y: view.bounds.height - footerView.frame.height,
198+
width: view.bounds.width,
199+
height: 100
200+
)
201+
202+
headerView.frame = CGRect(
203+
x: 0,
204+
y: 16,
205+
width: view.bounds.width,
206+
height: 100
207+
)
208+
}
209+
197210
open override var prefersStatusBarHidden: Bool {
198211
return LightboxConfig.hideStatusBar
199212
}
@@ -275,18 +288,8 @@ open class LightboxController: UIViewController {
275288
}
276289
}
277290

278-
let bounds = scrollView.bounds
279-
let headerViewHeight = headerView.closeButton.frame.height > headerView.deleteButton.frame.height
280-
? headerView.closeButton.frame.height
281-
: headerView.deleteButton.frame.height
282-
283-
headerView.frame = CGRect(x: 0, y: 16, width: bounds.width, height: headerViewHeight)
284-
footerView.frame = CGRect(x: 0, y: 0, width: bounds.width, height: 70)
285-
286291
[headerView, footerView].forEach { ($0 as AnyObject).configureLayout() }
287292

288-
footerView.frame.origin.y = bounds.height - footerView.frame.height
289-
290293
overlayView.frame = scrollView.frame
291294
overlayView.resizeGradientLayer()
292295
}
@@ -332,7 +335,7 @@ extension LightboxController: UIScrollViewDelegate {
332335
}
333336

334337
targetContentOffset.pointee.x = x
335-
currentPage = Int(x / screenBounds.width)
338+
currentPage = Int(x / view.bounds.width)
336339
}
337340
}
338341

@@ -398,7 +401,7 @@ extension LightboxController: HeaderViewDelegate {
398401

399402
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
400403
self.configureLayout()
401-
self.currentPage = Int(self.scrollView.contentOffset.x / self.screenBounds.width)
404+
self.currentPage = Int(self.scrollView.contentOffset.x / self.view.bounds.width)
402405
deleteButton.isEnabled = true
403406
}
404407
}
@@ -416,8 +419,6 @@ extension LightboxController: HeaderViewDelegate {
416419
extension LightboxController: FooterViewDelegate {
417420

418421
public func footerView(_ footerView: FooterView, didExpand expanded: Bool) {
419-
footerView.frame.origin.y = screenBounds.height - footerView.frame.height
420-
421422
UIView.animate(withDuration: 0.25, animations: {
422423
self.overlayView.alpha = expanded ? 1.0 : 0.0
423424
self.headerView.deleteButton.alpha = expanded ? 0.0 : 1.0

Source/Views/FooterView.swift

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ open class FooterView: UIView {
1616
label.delegate = self
1717

1818
return label
19-
}()
19+
}()
2020

2121
open fileprivate(set) lazy var pageLabel: UILabel = { [unowned self] in
2222
let label = UILabel(frame: CGRect.zero)
2323
label.isHidden = !LightboxConfig.PageIndicator.enabled
2424
label.numberOfLines = 1
2525

2626
return label
27-
}()
27+
}()
2828

2929
open fileprivate(set) lazy var separatorView: UIView = { [unowned self] in
3030
let view = UILabel(frame: CGRect.zero)
3131
view.isHidden = !LightboxConfig.PageIndicator.enabled
3232
view.backgroundColor = LightboxConfig.PageIndicator.separatorColor
3333

3434
return view
35-
}()
35+
}()
3636

3737
let gradientColors = [UIColor(hex: "040404").alpha(0.1), UIColor(hex: "040404")]
3838
open weak var delegate: FooterViewDelegate?
@@ -76,17 +76,29 @@ open class FooterView: UIView {
7676
}
7777
}
7878

79-
// MARK: - Layout
80-
81-
fileprivate func resetFrames() {
82-
frame.size.height = infoLabel.frame.height + 40 + 0.5
83-
84-
pageLabel.frame.origin = CGPoint(
85-
x: (frame.width - pageLabel.frame.width) / 2,
86-
y: frame.height - pageLabel.frame.height - 2)
79+
open override func layoutSubviews() {
80+
super.layoutSubviews()
81+
82+
do {
83+
let bottomPadding: CGFloat
84+
if #available(iOS 11, *) {
85+
bottomPadding = safeAreaInsets.bottom
86+
} else {
87+
bottomPadding = 0
88+
}
89+
90+
pageLabel.frame.origin = CGPoint(
91+
x: (frame.width - pageLabel.frame.width) / 2,
92+
y: frame.height - pageLabel.frame.height - 2 - bottomPadding
93+
)
94+
}
8795

88-
separatorView.frame = CGRect(x: 0, y: pageLabel.frame.minY - 2.5,
89-
width: frame.width, height: 0.5)
96+
separatorView.frame = CGRect(
97+
x: 0,
98+
y: pageLabel.frame.minY - 2.5,
99+
width: frame.width,
100+
height: 0.5
101+
)
90102

91103
infoLabel.frame.origin.y = separatorView.frame.minY - infoLabel.frame.height - 15
92104

@@ -107,7 +119,6 @@ extension FooterView: LayoutConfigurable {
107119
extension FooterView: InfoLabelDelegate {
108120

109121
public func infoLabel(_ infoLabel: InfoLabel, didExpand expanded: Bool) {
110-
resetFrames()
111122
_ = (expanded || infoLabel.fullText.isEmpty) ? removeGradientLayer() : addGradientLayer(gradientColors)
112123
delegate?.footerView(self, didExpand: expanded)
113124
}

Source/Views/HeaderView.swift

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ protocol HeaderViewDelegate: class {
66
}
77

88
open class HeaderView: UIView {
9-
10-
var centerTextStyle: NSMutableParagraphStyle = {
11-
var style = NSMutableParagraphStyle()
12-
style.alignment = .center
13-
return style
14-
}()
15-
169
open fileprivate(set) lazy var closeButton: UIButton = { [unowned self] in
1710
let title = NSAttributedString(
1811
string: LightboxConfig.CloseButton.text,
@@ -38,7 +31,7 @@ open class HeaderView: UIView {
3831
button.isHidden = !LightboxConfig.CloseButton.enabled
3932

4033
return button
41-
}()
34+
}()
4235

4336
open fileprivate(set) lazy var deleteButton: UIButton = { [unowned self] in
4437
let title = NSAttributedString(
@@ -65,7 +58,7 @@ open class HeaderView: UIView {
6558
button.isHidden = !LightboxConfig.DeleteButton.enabled
6659

6760
return button
68-
}()
61+
}()
6962

7063
weak var delegate: HeaderViewDelegate?
7164

@@ -99,9 +92,22 @@ open class HeaderView: UIView {
9992
extension HeaderView: LayoutConfigurable {
10093

10194
@objc public func configureLayout() {
102-
closeButton.frame.origin = CGPoint(
103-
x: bounds.width - closeButton.frame.width - 17, y: 0)
95+
let topPadding: CGFloat
10496

105-
deleteButton.frame.origin = CGPoint(x: 17, y: 0)
97+
if #available(iOS 11, *) {
98+
topPadding = safeAreaInsets.top
99+
} else {
100+
topPadding = 0
101+
}
102+
103+
closeButton.frame.origin = CGPoint(
104+
x: bounds.width - closeButton.frame.width - 17,
105+
y: topPadding
106+
)
107+
108+
deleteButton.frame.origin = CGPoint(
109+
x: 17,
110+
y: topPadding
111+
)
106112
}
107113
}

0 commit comments

Comments
 (0)