diff --git a/Demo/RootViewController.swift b/Demo/RootViewController.swift index 71e9d3b..f7c4319 100644 --- a/Demo/RootViewController.swift +++ b/Demo/RootViewController.swift @@ -49,9 +49,11 @@ final class RootViewController: UIViewController { appearance.textColor = .black appearance.font = .boldSystemFont(ofSize: 16) appearance.textInsets = UIEdgeInsets(top: 15, left: 20, bottom: 15, right: 20) - appearance.bottomOffsetPortrait = 100 + appearance.offsetPortrait = 10 + appearance.toastOrientation = .top appearance.cornerRadius = 20 appearance.maxWidthRatio = 0.7 + appearance.useSafeAreaForOffset = true } func configureAccessibility() { diff --git a/DemoObjC/RootViewController.m b/DemoObjC/RootViewController.m index 2e0c9ad..8f88e19 100644 --- a/DemoObjC/RootViewController.m +++ b/DemoObjC/RootViewController.m @@ -58,7 +58,7 @@ - (void)configureAppearance { appearance.textColor = UIColor.blackColor; appearance.font = [UIFont boldSystemFontOfSize: 16]; appearance.textInsets = UIEdgeInsetsMake(15, 20, 15, 20); - appearance.bottomOffsetPortrait = 100; + appearance.offsetPortrait = 100; appearance.cornerRadius = 20; appearance.maxWidthRatio = 0.7; } diff --git a/Sources/ToastOrientation.swift b/Sources/ToastOrientation.swift new file mode 100644 index 0000000..0d5f4b1 --- /dev/null +++ b/Sources/ToastOrientation.swift @@ -0,0 +1,13 @@ +// +// ToastOrientation.swift +// Toaster +// +// Created by Yongjun Lee on 2023/03/29. +// Copyright © 2023 Suyeol Jeon. All rights reserved. +// + +import Foundation +@objc public enum ToastOrientation: Int { + case top + case bottom +} diff --git a/Sources/ToastView.swift b/Sources/ToastView.swift index f337629..810799b 100644 --- a/Sources/ToastView.swift +++ b/Sources/ToastView.swift @@ -43,9 +43,11 @@ open class ToastView: UIView { get { return self.textLabel.font } set { self.textLabel.font = newValue } } + + @objc open dynamic var toastOrientation: ToastOrientation = .bottom /// The bottom offset from the screen's bottom in portrait mode. - @objc open dynamic var bottomOffsetPortrait: CGFloat = { + @objc open dynamic var offsetPortrait: CGFloat = { switch UIDevice.current.userInterfaceIdiom { // specific values case .phone: return 30 @@ -62,7 +64,7 @@ open class ToastView: UIView { }() /// The bottom offset from the screen's bottom in landscape mode. - @objc open dynamic var bottomOffsetLandscape: CGFloat = { + @objc open dynamic var offsetLandscape: CGFloat = { switch UIDevice.current.userInterfaceIdiom { // specific values case .phone: return 20 @@ -81,7 +83,7 @@ open class ToastView: UIView { /// 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 useSafeAreaForOffset: 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 +200,27 @@ open class ToastView: UIView { if orientation.isPortrait || !ToastWindow.shared.shouldRotateManually { width = containerSize.width height = containerSize.height - y = self.bottomOffsetPortrait + y = self.offsetPortrait } else { width = containerSize.height height = containerSize.width - y = self.bottomOffsetLandscape + y = self.offsetLandscape } - if #available(iOS 11.0, *), useSafeAreaForBottomOffset { - y += ToastWindow.shared.safeAreaInsets.bottom + if #available(iOS 11.0, *), useSafeAreaForOffset { + switch self.toastOrientation { + case .bottom: + y += ToastWindow.shared.safeAreaInsets.bottom + case .top: + y += ToastWindow.shared.safeAreaInsets.top + } } let backgroundViewSize = self.backgroundView.frame.size x = (width - backgroundViewSize.width) * 0.5 - y = height - (backgroundViewSize.height + y) + if self.toastOrientation == .bottom { + y = height - (backgroundViewSize.height + y) + } + self.frame = CGRect( x: x, y: y, diff --git a/Toaster.xcodeproj/project.pbxproj b/Toaster.xcodeproj/project.pbxproj index 56bd660..ae0e8c9 100644 --- a/Toaster.xcodeproj/project.pbxproj +++ b/Toaster.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ 0397CD2D1D85D6920077D82F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0306DB991D85D626007AE314 /* AppDelegate.swift */; }; 0397CD2E1D85D6930077D82F /* RootViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0306DB9E1D85D626007AE314 /* RootViewController.swift */; }; 03C23F3C1D8823AA00EE8424 /* Toaster.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03A75E2B1BCF2066002E46C4 /* Toaster.framework */; }; + 18C31E1E29D416C300E663E3 /* ToastOrientation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18C31E1D29D416C300E663E3 /* ToastOrientation.swift */; }; 3706852A20D6A68800F09B36 /* Toaster.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 03A75E2B1BCF2066002E46C4 /* Toaster.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 3719A72C225598E4009EAFCD /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 3719A72B225598E4009EAFCD /* AppDelegate.m */; }; 3719A72F225598E4009EAFCD /* RootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3719A72E225598E4009EAFCD /* RootViewController.m */; }; @@ -94,6 +95,7 @@ 0306DBB01D85D62E007AE314 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 03A75E2B1BCF2066002E46C4 /* Toaster.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Toaster.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 03A75E4F1BCF20D4002E46C4 /* ToasterDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ToasterDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 18C31E1D29D416C300E663E3 /* ToastOrientation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastOrientation.swift; sourceTree = ""; }; 3719A728225598E4009EAFCD /* ToasterDemoObjC.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ToasterDemoObjC.app; sourceTree = BUILT_PRODUCTS_DIR; }; 3719A72A225598E4009EAFCD /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 3719A72B225598E4009EAFCD /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; @@ -167,6 +169,7 @@ 0306DBA91D85D62A007AE314 /* ToastWindow.swift */, C1C0178C23163F4F00446CED /* KeyboardObserver.swift */, C1C017912316419900446CED /* UIApplication+Load.swift */, + 18C31E1D29D416C300E663E3 /* ToastOrientation.swift */, ); path = Sources; sourceTree = ""; @@ -404,6 +407,7 @@ buildActionMask = 2147483647; files = ( 0306DBAC1D85D62A007AE314 /* ToastCenter.swift in Sources */, + 18C31E1E29D416C300E663E3 /* ToastOrientation.swift in Sources */, 0306DBAE1D85D62A007AE314 /* ToastWindow.swift in Sources */, C1C017922316419900446CED /* UIApplication+Load.swift in Sources */, 0306DBAB1D85D62A007AE314 /* Toast.swift in Sources */,