Skip to content
This repository was archived by the owner on Jun 17, 2018. It is now read-only.

Commit 2c1fe4b

Browse files
authored
Merge pull request #287 from kitasuke/avoid_force_unwrapping
Avoid force unwrapping
2 parents 8615e8b + d5e6075 commit 2c1fe4b

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Pod/Classes/MenuItemView.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,11 @@ open class MenuItemView: UIView {
229229
let width: CGFloat
230230
switch menuOptions.displayMode {
231231
case .segmentedControl:
232-
width = UIApplication.shared.keyWindow!.bounds.size.width / CGFloat(menuOptions.itemsOptions.count)
232+
if let windowWidth = UIApplication.shared.keyWindow?.bounds.size.width {
233+
width = windowWidth / CGFloat(menuOptions.itemsOptions.count)
234+
} else {
235+
width = UIScreen.main.bounds.width / CGFloat(menuOptions.itemsOptions.count)
236+
}
233237
default:
234238
width = image.size.width + horizontalMargin * 2
235239
}

Pod/Classes/MenuView.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ open class MenuView: UIScrollView {
5656
}
5757
}
5858
fileprivate var centerOfScreenWidth: CGFloat {
59-
return menuItemViews[currentPage].frame.midX - UIApplication.shared.keyWindow!.bounds.width / 2
59+
let screenWidth: CGFloat
60+
if let width = UIApplication.shared.keyWindow?.bounds.width {
61+
screenWidth = width
62+
} else {
63+
screenWidth = UIScreen.main.bounds.width
64+
}
65+
return menuItemViews[currentPage].frame.midX - screenWidth / 2
6066
}
6167
fileprivate var contentOffsetXForCurrentPage: CGFloat {
6268
guard menuItemCount > MinimumSupportedViewCount else { return 0.0 }
@@ -307,8 +313,8 @@ open class MenuView: UIScrollView {
307313
default: return
308314
}
309315

310-
let firstMenuView = menuItemViews.first!
311-
let lastMenuView = menuItemViews.last!
316+
guard let firstMenuView = menuItemViews.first,
317+
let lastMenuView = menuItemViews.last else { return }
312318

313319
var inset = contentInset
314320
let halfWidth = frame.width / 2

0 commit comments

Comments
 (0)