diff --git a/Demo/Info.plist b/Demo/Info.plist index eabb3ae..026e162 100644 --- a/Demo/Info.plist +++ b/Demo/Info.plist @@ -41,5 +41,7 @@ UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight + NSCameraUsageDescription + App want to use the camera :) diff --git a/Demo/RootViewController.swift b/Demo/RootViewController.swift index 71e9d3b..09014a8 100644 --- a/Demo/RootViewController.swift +++ b/Demo/RootViewController.swift @@ -31,12 +31,20 @@ final class RootViewController: UIViewController { imagePickerButton.center = CGPoint(x: view.center.x, y: 155) self.view.addSubview(imagePickerButton) + let showCameraButton = UIButton(type: .system) + showCameraButton.setTitle("Show Camera", for: .normal) + showCameraButton.sizeToFit() + showCameraButton.addTarget(self, action: #selector(self.showCameraButtonTouchUpInside), for: .touchUpInside) + showCameraButton.autoresizingMask = [.flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin] + showCameraButton.center = CGPoint(x: view.center.x, y: 185) + self.view.addSubview(showCameraButton) + let shadowButton = UIButton(type: .system) shadowButton.setTitle("Toggle shadow", for: .normal) shadowButton.sizeToFit() shadowButton.addTarget(self, action: #selector(self.shadowButtonTouchUpInside), for: .touchUpInside) shadowButton.autoresizingMask = [.flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin] - shadowButton.center = CGPoint(x: view.center.x, y: 185) + shadowButton.center = CGPoint(x: view.center.x, y: 215) self.view.addSubview(shadowButton) self.configureAppearance() @@ -59,7 +67,7 @@ final class RootViewController: UIViewController { } @objc dynamic func showButtonTouchUpInside() { - Toast(text: "Basic Toast").show() + Toast(text: "Basic Toast").show(position: .top) Toast(attributedText: NSAttributedString(string: "AttributedString Toast", attributes: [NSAttributedString.Key.backgroundColor: UIColor.yellow])).show() Toast(text: "You can set duration. `Delay.short` means 2 seconds.\n" + "`Delay.long` means 3.5 seconds.", @@ -79,6 +87,10 @@ final class RootViewController: UIViewController { pickImage() } + @objc dynamic func showCameraButtonTouchUpInside(sender: UIButton) { + showCamera() + } + @objc dynamic func shadowButtonTouchUpInside(sender: UIButton) { let appearance = ToastView.appearance() if appearance.shadowColor == nil { @@ -115,6 +127,14 @@ final class RootViewController: UIViewController { } } let mediaPicker = UIImagePickerController() + mediaPicker.sourceType = .camera + mediaPicker.delegate = self + present(mediaPicker, animated: true, completion: nil) + } + + private func showCamera() { + let mediaPicker = UIImagePickerController() + mediaPicker.sourceType = .camera mediaPicker.delegate = self present(mediaPicker, animated: true, completion: nil) } diff --git a/Sources/Toast.swift b/Sources/Toast.swift index ce8b7cc..30320e6 100644 --- a/Sources/Toast.swift +++ b/Sources/Toast.swift @@ -94,7 +94,8 @@ open class Toast: Operation { // MARK: Showing - @objc public func show() { + @objc public func show(position: ToastPosition = .bottom) { + self.view.position = position ToastCenter.default.add(self) } diff --git a/Sources/ToastView.swift b/Sources/ToastView.swift index f337629..5d49bd8 100644 --- a/Sources/ToastView.swift +++ b/Sources/ToastView.swift @@ -1,5 +1,10 @@ import UIKit +@objc public enum ToastPosition: Int, RawRepresentable { + case top + case bottom +} + open class ToastView: UIView { // MARK: Properties @@ -13,7 +18,6 @@ open class ToastView: UIView { get { return self.textLabel.attributedText } set { self.textLabel.attributedText = newValue } } - // MARK: Appearance @@ -78,10 +82,14 @@ open class ToastView: UIView { } }() + @objc open dynamic var topOffset: CGFloat = 60 + + @objc open dynamic var position: ToastPosition = .bottom + /// If this value is `true` and SafeArea is available, /// `safeAreaInsets.bottom` will be added to the `bottomOffsetPortrait` and `bottomOffsetLandscape`. /// Default value: false - @objc open dynamic var useSafeAreaForBottomOffset: Bool = false + @objc open dynamic var useSafeAreaOffset: Bool = false /// The width ratio of toast view in window, specified as a value from 0.0 to 1.0. /// Default value: 0.875 @@ -198,19 +206,19 @@ open class ToastView: UIView { if orientation.isPortrait || !ToastWindow.shared.shouldRotateManually { width = containerSize.width height = containerSize.height - y = self.bottomOffsetPortrait + y = self.position == .top ? self.topOffset : self.bottomOffsetPortrait } else { width = containerSize.height height = containerSize.width - y = self.bottomOffsetLandscape + y = self.position == .top ? self.topOffset : self.bottomOffsetLandscape } - if #available(iOS 11.0, *), useSafeAreaForBottomOffset { - y += ToastWindow.shared.safeAreaInsets.bottom + if #available(iOS 11.0, *), useSafeAreaOffset { + y += self.position == .top ? ToastWindow.shared.safeAreaInsets.top : ToastWindow.shared.safeAreaInsets.bottom } let backgroundViewSize = self.backgroundView.frame.size x = (width - backgroundViewSize.width) * 0.5 - y = height - (backgroundViewSize.height + y) + y = self.position == .top ? y : height - (backgroundViewSize.height + y) self.frame = CGRect( x: x, y: y,