Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Toast position (bottom and top) #221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Demo/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSCameraUsageDescription</key>
<string>App want to use the camera :)</string>
</dict>
</plist>
24 changes: 22 additions & 2 deletions Demo/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.",
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Toast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
22 changes: 15 additions & 7 deletions Sources/ToastView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import UIKit

@objc public enum ToastPosition: Int, RawRepresentable {
case top
case bottom
}

open class ToastView: UIView {

// MARK: Properties
Expand All @@ -13,7 +18,6 @@ open class ToastView: UIView {
get { return self.textLabel.attributedText }
set { self.textLabel.attributedText = newValue }
}


// MARK: Appearance

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down