스위프트에서 토스트 메시지를 만드는 방법은 무엇입니까?
빨리 건배사를 할 수 있는 방법이 없을까요?
객관적인 c로 시도해봤지만 빠른 해결책을 찾을 수 없었습니다.
[self.view makeToast:@"Account created Successfully"
duration:0.5
position:@"bottom"];
extension UIViewController {
func showToast(message : String, font: UIFont) {
let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y: self.view.frame.size.height-100, width: 150, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.font = font
toastLabel.textAlignment = .center;
toastLabel.text = message
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
} }
다음과 같이 사용:
self.showToast(message: "Your Toast Message", font: .systemFont(ofSize: 12.0))
스위프트 4용
레이아웃 제약 조건을 사용하는 내 버전의 토스트는 모든 텍스트 크기에 그대로 적용됩니다(Tony Franzis의 응답에 기반함).
전화만 하면 됩니다.Toast.show(message: "My message", myViewControllerName)
class Toast {
static func show(message: String, controller: UIViewController) {
let toastContainer = UIView(frame: CGRect())
toastContainer.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastContainer.alpha = 0.0
toastContainer.layer.cornerRadius = 25;
toastContainer.clipsToBounds = true
let toastLabel = UILabel(frame: CGRect())
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.font.withSize(12.0)
toastLabel.text = message
toastLabel.clipsToBounds = true
toastLabel.numberOfLines = 0
toastContainer.addSubview(toastLabel)
controller.view.addSubview(toastContainer)
toastLabel.translatesAutoresizingMaskIntoConstraints = false
toastContainer.translatesAutoresizingMaskIntoConstraints = false
let a1 = NSLayoutConstraint(item: toastLabel, attribute: .leading, relatedBy: .equal, toItem: toastContainer, attribute: .leading, multiplier: 1, constant: 15)
let a2 = NSLayoutConstraint(item: toastLabel, attribute: .trailing, relatedBy: .equal, toItem: toastContainer, attribute: .trailing, multiplier: 1, constant: -15)
let a3 = NSLayoutConstraint(item: toastLabel, attribute: .bottom, relatedBy: .equal, toItem: toastContainer, attribute: .bottom, multiplier: 1, constant: -15)
let a4 = NSLayoutConstraint(item: toastLabel, attribute: .top, relatedBy: .equal, toItem: toastContainer, attribute: .top, multiplier: 1, constant: 15)
toastContainer.addConstraints([a1, a2, a3, a4])
let c1 = NSLayoutConstraint(item: toastContainer, attribute: .leading, relatedBy: .equal, toItem: controller.view, attribute: .leading, multiplier: 1, constant: 65)
let c2 = NSLayoutConstraint(item: toastContainer, attribute: .trailing, relatedBy: .equal, toItem: controller.view, attribute: .trailing, multiplier: 1, constant: -65)
let c3 = NSLayoutConstraint(item: toastContainer, attribute: .bottom, relatedBy: .equal, toItem: controller.view, attribute: .bottom, multiplier: 1, constant: -75)
controller.view.addConstraints([c1, c2, c3])
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseIn, animations: {
toastContainer.alpha = 1.0
}, completion: { _ in
UIView.animate(withDuration: 0.5, delay: 1.5, options: .curveEaseOut, animations: {
toastContainer.alpha = 0.0
}, completion: {_ in
toastContainer.removeFromSuperview()
})
})
}
}
코드 한 줄로 사용자 정의 가능한 토스트 알림을 지원하는 타사 라이브러리가 있습니다.다음은 간단한 예입니다.
import Toast_Swift
...
// basic usage
self.view.makeToast("This is a piece of toast")
// toast with a specific duration and position
self.view.makeToast("This is a piece of toast", duration: 3.0, position: .top)
https://github.com/scalessec/Toast-Swift
(Swift 3/4+용으로 업데이트됨)
Swift 5에 대한 솔루션이 두 가지 더 있습니다.
최선의 해결책 (제 의견)
장점:
- 화면을 회전할 때 올바르게 작동합니다.
- 구속조건은 배치에 사용됩니다.
- SafeArea와 올바르게 작동합니다.
단점:
- 확장이 필요합니다.
UILabel
들여쓰기를 추가할 클래스입니다.이것 없이도 사람들은 그것을 놓을 수 있습니다.UILabel
에 시대에UIVIew
.
코드:
class ToastLabel: UILabel {
var textInsets = UIEdgeInsets.zero {
didSet { invalidateIntrinsicContentSize() }
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let insetRect = bounds.inset(by: textInsets)
let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
let invertedInsets = UIEdgeInsets(top: -textInsets.top, left: -textInsets.left, bottom: -textInsets.bottom, right: -textInsets.right)
return textRect.inset(by: invertedInsets)
}
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: textInsets))
}
}
extension UIViewController {
static let DELAY_SHORT = 1.5
static let DELAY_LONG = 3.0
func showToast(_ text: String, delay: TimeInterval = DELAY_LONG) {
let label = ToastLabel()
label.backgroundColor = UIColor(white: 0, alpha: 0.5)
label.textColor = .white
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 15)
label.alpha = 0
label.text = text
label.clipsToBounds = true
label.layer.cornerRadius = 20
label.numberOfLines = 0
label.textInsets = UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 15)
label.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(label)
let saveArea = view.safeAreaLayoutGuide
label.centerXAnchor.constraint(equalTo: saveArea.centerXAnchor, constant: 0).isActive = true
label.leadingAnchor.constraint(greaterThanOrEqualTo: saveArea.leadingAnchor, constant: 15).isActive = true
label.trailingAnchor.constraint(lessThanOrEqualTo: saveArea.trailingAnchor, constant: -15).isActive = true
label.bottomAnchor.constraint(equalTo: saveArea.bottomAnchor, constant: -30).isActive = true
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseIn, animations: {
label.alpha = 1
}, completion: { _ in
UIView.animate(withDuration: 0.5, delay: delay, options: .curveEaseOut, animations: {
label.alpha = 0
}, completion: {_ in
label.removeFromSuperview()
})
})
}
}
사용 방법:
class MyController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
showToast("Message")
}
}
기타해
장점:
- 이 버전에서는 다음에 대한 바인딩을 사용하지 않습니다.
UIViewController
단점:
- 화면 회전 후에는 레이블이 움직이지 않습니다.
- 다중 줄 문자열에서 올바르게 작동하지 않습니다.
코드:
class Helper {
static let DELAY_SHORT = 1.5
static let DELAY_LONG = 3.0
static func showToast(_ text: String, delay: TimeInterval = DELAY_LONG) {
guard let window = UIApplication.shared.keyWindow else {
return
}
let label = BaseLabel()
label.backgroundColor = UIColor(white: 0, alpha: 0.5)
label.textColor = .white
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 15)
label.alpha = 0
label.text = text
label.numberOfLines = 0
var vertical: CGFloat = 0
var size = label.intrinsicContentSize
var width = min(size.width, window.frame.width - 60)
if width != size.width {
vertical = 10
label.textAlignment = .justified
}
label.textInsets = UIEdgeInsets(top: vertical, left: 15, bottom: vertical, right: 15)
size = label.intrinsicContentSize
width = min(size.width, window.frame.width - 60)
label.frame = CGRect(x: 20, y: window.frame.height - 90, width: width, height: size.height + 20)
label.center.x = window.center.x
label.layer.cornerRadius = min(label.frame.height/2, 25)
label.layer.masksToBounds = true
window.addSubview(label)
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseIn, animations: {
label.alpha = 1
}, completion: { _ in
UIView.animate(withDuration: 0.5, delay: delay, options: .curveEaseOut, animations: {
label.alpha = 0
}, completion: {_ in
label.removeFromSuperview()
})
})
}
}
사용 방법:
Helper.showToast("Message")
아래의 방법만 추가하면 됩니다.애니메이션과 함께 다양한 색상의 메시지가 표시됩니다(메시지가 왼쪽에서 오른쪽으로 표시되고 사라짐).
스위프트 3.0 -
class Toast
{
class private func showAlert(backgroundColor:UIColor, textColor:UIColor, message:String)
{
let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
let label = UILabel(frame: CGRect.zero)
label.textAlignment = NSTextAlignment.center
label.text = message
label.font = UIFont(name: "", size: 15)
label.adjustsFontSizeToFitWidth = true
label.backgroundColor = backgroundColor //UIColor.whiteColor()
label.textColor = textColor //TEXT COLOR
label.sizeToFit()
label.numberOfLines = 4
label.layer.shadowColor = UIColor.gray.cgColor
label.layer.shadowOffset = CGSize(width: 4, height: 3)
label.layer.shadowOpacity = 0.3
label.frame = CGRect(x: appDelegate.window!.frame.size.width, y: 64, width: appDelegate.window!.frame.size.width, height: 44)
label.alpha = 1
appDelegate.window!.addSubview(label)
var basketTopFrame: CGRect = label.frame;
basketTopFrame.origin.x = 0;
UIView.animate(withDuration
:2.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.1, options: UIViewAnimationOptions.curveEaseOut, animations: { () -> Void in
label.frame = basketTopFrame
}, completion: {
(value: Bool) in
UIView.animate(withDuration:2.0, delay: 2.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.1, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in
label.alpha = 0
}, completion: {
(value: Bool) in
label.removeFromSuperview()
})
})
}
class func showPositiveMessage(message:String)
{
showAlert(backgroundColor: UIColor.green, textColor: UIColor.white, message: message)
}
class func showNegativeMessage(message:String)
{
showAlert(backgroundColor: UIColor.red, textColor: UIColor.white, message: message)
}
}
스위프트 5.간단한 토스트 구현을 원하시면 아래 코드를 찾아주세요.
extension UIViewController{
func showToast(message : String, seconds: Double){
let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
alert.view.backgroundColor = .black
alert.view.alpha = 0.5
alert.view.layer.cornerRadius = 15
self.present(alert, animated: true)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + seconds) {
alert.dismiss(animated: true)
}
}
}
UIView 컨트롤러에서 호출
self.showToast(message: "Updating...", seconds: 1.0)
정확히 당신에게 필요한 것은 https://github.com/Rannie/Toast-Swift/blob/master/SwiftToastDemo/Toast/HRToast%2BUIView.swift 입니다.
HRTast + UIView.swift 클래스를 다운로드하고 프로젝트로 드래그 앤 드롭합니다.대화 상자에서 '필요한 경우 항목 복사'를 선택해야 합니다.
//Usage:
self.view.makeToast(message: "Simple Toast")
self.view.makeToast(message: "Simple Toast", duration: 2.0, position:HRToastPositionTop)
self.view.makeToast(message: "Simple Toast", duration: 2.0, position: HRToastPositionCenter, image: UIImage(named: "ic_120x120")!)
self.view.makeToast(message: "It is just awesome", duration: 2.0, position: HRToastPositionDefault, title: "Simple Toast")
self.view.makeToast(message: "It is just awesome", duration: 2.0, position: HRToastPositionCenter, title: "Simple Toast", image: UIImage(named: "ic_120x120")!)
self.view.makeToastActivity()
self.view.makeToastActivity(position: HRToastPositionCenter)
self.view.makeToastActivity(position: HRToastPositionDefault, message: "Loading")
self.view.makeToastActivityWithMessage(message: "Loading")
안드로이드처럼 토스트 메시지가 필요할 때마다 이 확장자를 사용해 왔습니다.프로젝트에 확장을 복사한 다음 UIViewController 클래스에서 다음과 같은 함수를 호출합니다.
self.toastMessage("Downloading...")
// Extention is below
extension UIViewController {
func toastMessage(_ message: String){
guard let window = UIApplication.shared.keyWindow else {return}
let messageLbl = UILabel()
messageLbl.text = message
messageLbl.textAlignment = .center
messageLbl.font = UIFont.systemFont(ofSize: 12)
messageLbl.textColor = .white
messageLbl.backgroundColor = UIColor(white: 0, alpha: 0.5)
let textSize:CGSize = messageLbl.intrinsicContentSize
let labelWidth = min(textSize.width, window.frame.width - 40)
messageLbl.frame = CGRect(x: 20, y: window.frame.height - 90, width: labelWidth + 30, height: textSize.height + 20)
messageLbl.center.x = window.center.x
messageLbl.layer.cornerRadius = messageLbl.frame.height/2
messageLbl.layer.masksToBounds = true
window.addSubview(messageLbl)
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
UIView.animate(withDuration: 1, animations: {
messageLbl.alpha = 0
}) { (_) in
messageLbl.removeFromSuperview()
}
}
}}
글꼴, 정렬, 텍스트 색상 등의 고급 사용자 지정 없이 간단한 토스트 메시지가 필요한 경우 다음과 같이 하면 됩니다.
let messageVC = UIAlertController(title: "Message Title", message: "Account Created successfully" , preferredStyle: .actionSheet)
present(messageVC, animated: true) {
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false, block: { (_) in
messageVC.dismiss(animated: true, completion: nil)})}
.actionSheet
화면 하단의 경고를 표시하고 타이머가 표시 기간을 관리합니다.이것을 UIViewController의 확장으로 추가한 다음 어디서나 호출할 수 있습니다.
한다면makeToast:duration:position:
목표-c에서 정의되며 호출될 수 있으며, 스위프트 코드는
self.view.makeToast("Acount created Successfully", duration: 0.5, position: "bottom")
그러나 빠른 코드에서 이러한 메서드에 액세스하려면 브리징 헤더를 사용해야 할 수도 있습니다.
@mr-bean 코드가 최신 Swift 버전(3.x)으로 업데이트됨
let toastLabel =
UILabel(frame:
CGRect(x: self.view.frame.size.width/2 - 150,
y: self.view.frame.size.height-100,
width: 300,
height: 35))
toastLabel.backgroundColor = UIColor.black
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = NSTextAlignment.center
self.view.addSubview(toastLabel)
toastLabel.text = message
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
UIView.animate(withDuration: 4.0, animations: {
toastLabel.alpha = 0.0
})
저는 @samo의 답변을 다음과 같이 수정했습니다.
-
적절한 변수 이름
-
선행 및 후행 제약 조건이 중앙 제약 조건으로 변경되었습니다.
이제 메시지가 메시지에 따라 폭을 조정하고 중앙에 배치됩니다.
extension UIViewController {
func showToast(message: String) {
let toastContainer = UIView(frame: CGRect())
toastContainer.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastContainer.alpha = 0.0
toastContainer.layer.cornerRadius = 20;
toastContainer.clipsToBounds = true
let toastLabel = UILabel(frame: CGRect())
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.font.withSize(12.0)
toastLabel.text = message
toastLabel.clipsToBounds = true
toastLabel.numberOfLines = 0
toastContainer.addSubview(toastLabel)
self.view.addSubview(toastContainer)
toastLabel.translatesAutoresizingMaskIntoConstraints = false
toastContainer.translatesAutoresizingMaskIntoConstraints = false
let centerX = NSLayoutConstraint(item: toastLabel, attribute: .centerX, relatedBy: .equal, toItem: toastContainer, attribute: .centerXWithinMargins, multiplier: 1, constant: 0)
let lableBottom = NSLayoutConstraint(item: toastLabel, attribute: .bottom, relatedBy: .equal, toItem: toastContainer, attribute: .bottom, multiplier: 1, constant: -15)
let lableTop = NSLayoutConstraint(item: toastLabel, attribute: .top, relatedBy: .equal, toItem: toastContainer, attribute: .top, multiplier: 1, constant: 15)
toastContainer.addConstraints([centerX, lableBottom, lableTop])
let containerCenterX = NSLayoutConstraint(item: toastContainer, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)
let containerTrailing = NSLayoutConstraint(item: toastContainer, attribute: .width, relatedBy: .equal, toItem: toastLabel, attribute: .width, multiplier: 1.1, constant: 0)
let containerBottom = NSLayoutConstraint(item: toastContainer, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: -75)
self.view.addConstraints([containerCenterX,containerTrailing, containerBottom])
UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseIn, animations: {
toastContainer.alpha = 1.0
}, completion: { _ in
UIView.animate(withDuration: 0.5, delay: 1.5, options: .curveEaseOut, animations: {
toastContainer.alpha = 0.0
}, completion: {_ in
toastContainer.removeFromSuperview()
})
})
}
}
저는 인정된 답이 있다는 것을 알지만, 그것들은 모두 큰 결점이 있는 것처럼 보입니다 - 만약 당신이 짧은 시간 안에 몇 개의 건배사를 보여주면 그것들은 서로 위에 나타날 것입니다.다음은 이 문제를 고려한 구현입니다.
class Toast: UILabel {
private let BOTTOM_MARGIN: CGFloat = 16
private let SIDE_MARGIN: CGFloat = 16
private let HEIGHT: CGFloat = 35
private let SHOW_TIME_SECONDS = TimeInterval(3)
private let BACKGROUND_COLOR = UIColor.darkGray.withAlphaComponent(0.7).cgColor
private let TEXT_COLOR = UIColor.white
private let ANIMATION_DURATION_SEC = 0.33
private static var queue: [ToastHolder] = []
private static var showing: Toast?
init(_ text: String) {
super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
self.text = text
self.textColor = TEXT_COLOR
textAlignment = .center
self.layer.backgroundColor = BACKGROUND_COLOR
self.layer.cornerRadius = 5
}
public func show(_ parent: UIViewController) {
frame = CGRect(x: SIDE_MARGIN, y: UIScreen.main.bounds.height - BOTTOM_MARGIN - HEIGHT, width: UIScreen.main.bounds.width - 2 * SIDE_MARGIN, height: HEIGHT)
if Toast.showing == nil {
Log.d("showing \(String(describing: text))")
Toast.showing = self
alpha = 0
parent.view.addSubview(self)
UIView.animate(withDuration: ANIMATION_DURATION_SEC, animations: {
self.alpha = 1
}, completion: { (completed) in
Timer.scheduledTimer(timeInterval: self.SHOW_TIME_SECONDS, target: self, selector: #selector(self.onTimeout), userInfo: nil, repeats: false)
})
} else {
Toast.queue.append(ToastHolder(self, parent))
}
}
@objc func onTimeout() {
UIView.animate(withDuration: ANIMATION_DURATION_SEC, animations: {
self.alpha = 0
}, completion: { (completed) in
Toast.showing = nil
self.removeFromSuperview()
if !Toast.queue.isEmpty {
let holder = Toast.queue.removeFirst()
holder.toast.show(holder.parent)
}
})
}
required init?(coder aDecoder: NSCoder) {
fatalError("this initializer is not supported")
}
private class ToastHolder {
let toast: Toast
let parent: UIViewController
init(_ t: Toast, _ p: UIViewController) {
toast = t
parent = p
}
}
}
용도:
Toast("my message").show(self)
누군가에게 도움이 되길 바랍니다.
토스터를 사용하는 것은 어떻습니까?
한눈에 보기
Toast(text: "Hello, world!").show()
지연 및 지속 시간 설정
Toast(text: "Hello, world!", duration: Delay.long)
Toast(text: "Hello, world!", delay: Delay.short, duration: Delay.long)
토스트 제거
let toast = Toast(text: "Hello")
toast.show()
toast.cancel() // remove toast immediately
모양 사용자 지정
- 배경색
- 모서리 반지름
- 텍스트 집합
- 텍스트 색상
- 폰트
- 하단 간격띄우기 초상화
- 하단 간격띄우기 풍경
- 섀도 경로
- 그림자 색
- 그림자 불투명도
- 그림자 간격띄우기
- 그림자 반지름
- 최대 너비 비율
- 하단 간격띄우기에 안전 영역 사용
사용하는 대신UILabel
사용.UITextView
더 좋은 결과를 얻을 수 있습니다.
func showToast(message: String) {
let toastLabel = UITextView(frame: CGRect(x: self.view.frame.size.width/16, y: self.view.frame.size.height-150, width: self.view.frame.size.width * 7/8, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.text = " \(message) "
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
toastLabel.font = UIFont(name: (toastLabel.font?.fontName)!, size: 16)
toastLabel.layoutEdgeInsets.left = 8
toastLabel.layoutEdgeInsets.right = 8
toastLabel.center.x = self.view.frame.size.width/2
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 5.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
}
양쪽 끝에 간격을 잘 두어 보기 좋게 하기 위해 공간에 메시지를 추가합니다.Mr.의 답변 수정 버전.콩
extension UIViewController {
func showToast(message:String,color:UIColor) {
DispatchQueue.main.async {
let toastLbl = UILabel(frame: CGRect(x: 20, y: self.view.frame.size.height - 100, width: self.view.frame.width - 40 , height: 30))
toastLbl.layer.cornerRadius = 8;
toastLbl.clipsToBounds = true
toastLbl.textColor = .white
toastLbl.font = .systemFont(ofSize: 15)
toastLbl.textAlignment = .center;
toastLbl.text = message
self.view.addSubview(toastLbl)
toastLbl.backgroundColor = color
UIView.animate(withDuration: 2.0, delay: 0, options: .transitionCurlDown, animations: {
}, completion: {(isCompleted) in
toastLbl.removeFromSuperview()
})
}
} }
self.view.showToast(message: "Toast Shown", color: .green)
static func popUp(context ctx: UIViewController, msg: String) {
let toast = UILabel(frame:
CGRect(x: 16, y: ctx.view.frame.size.height / 2,
width: ctx.view.frame.size.width - 32, height: 100))
toast.backgroundColor = UIColor.lightGray
toast.textColor = UIColor.white
toast.textAlignment = .center;
toast.numberOfLines = 3
toast.font = UIFont.systemFont(ofSize: 20)
toast.layer.cornerRadius = 12;
toast.clipsToBounds = true
toast.text = msg
ctx.view.addSubview(toast)
UIView.animate(withDuration: 5.0, delay: 0.2,
options: .curveEaseOut, animations: {
toast.alpha = 0.0
}, completion: {(isCompleted) in
toast.removeFromSuperview()
})
}
그러면 UIViewController에서 호출하면 됩니다.
popUp(context: self, msg: "Your message")
다음 답변에 추가하겠습니다.이 라이브러리는 화면 상단 또는 하단에서 토스트 메시지를 제공할 수 있도록 DCTastView에 필요한 작업을 수행합니다.
포드를 추가하기만 하면 됩니다.
pod 'DCToastView'
사용할 위치로 가져옵니다.
import DCToastView
그리고 그것을 사용하세요.
ToastPresenter.shared.show(in: self.view, message: "This is a toast")
다음 속성을 표시 방법으로 전달할 수 있습니다.
- 보기: 토스트를 보여줄 보기
- 메시지:토스트가 보여줄 메시지
- 토스트 장소:.down 또는 .up일 수 있는 장소
- 배경색:토스트의 배경색. 기본값은 검은색입니다.
- 텍스트 색상:메시지의 텍스트 색상. 기본값은 흰색입니다.
- 제한 시간:제공되지 않은 경우 토스트가 꺼지는 시간(초)은 토스트가 끈적끈적한 상태(터치할 때까지 유지됨)를 의미합니다. 기본값은 0입니다.
- 둥글기:토스트의 둥근 모양: .none, .low, .mid, .high, 기본값은 .mid입니다.
이것은 적절한 패딩으로 중앙에서 토스트를 만드는 데 도움이 될 것입니다.
func showToast(message:String,view:UIView){
let toastLabel = PaddingLabel()
toastLabel.frame = CGRect(x:0, y: view.frame.size.height-100, width: view.frame.width-50, height: 0)
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.font = UIFont(name: "Montserrat-Light", size: 12.0)
toastLabel.text = message
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
toastLabel.sizeToFit()
toastLabel.frame.origin.x=(view.frame.width/2)-(toastLabel.frame.width/2)
view.addSubview(toastLabel)
UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
}
그리고 레이블 패딩에 대해 이 PaddingLabel 파일을 추가합니다.
import Foundation
import UIKit
class PaddingLabel: UILabel {
let padding=UIEdgeInsetsMake(5, 10, 5,10)
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, padding))
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
let superSizeThatFits=super.sizeThatFits(size)
let width=superSizeThatFits.width+padding.left+padding.right
let height=superSizeThatFits.height+padding.top+padding.bottom
return CGSize(width: width, height: height)
}
}
미스터 빈스의 대답은 잘 작동합니다.하지만, 그의 대답은 폭이 작고 여러 줄에 친숙하지 않습니다.대신 이것을 사용합니다.
func showToastFaded(message : String) {
let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 125, y: self.view.frame.size.height-100, width: 250, height: 35))
toastLabel.numberOfLines = 0
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.text = message
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
toastLabel.sizeToFit()
toastLabel.frame = CGRect( x: toastLabel.frame.minX, y: toastLabel.frame.minY,width: toastLabel.frame.width + 20, height: toastLabel.frame.height + 8)
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 4.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
}
iOS 앱에서 토스트를 추가하는 가장 좋고 쉬운 방법은 Loopjet이라는 라이브러리를 사용하는 것입니다.라이브러리가 매우 작기 때문에(27kb) 프로젝트에 큰 영향을 미치지 않습니다.해보세요!
저는 @Samo 답변을 바꿨습니다.여기 있습니다, 제 것은, 꽤 간단합니다.이것은 또한 풍경화이고 긴 텍스트 친화적입니다.
Toast.show(message: "Hello Toast", on: view)
..
class Toast {
static func show(message: String, on baseView: UIView) {
let containerView = UIView()
containerView.backgroundColor = .black.withAlphaComponent(0.6)
containerView.alpha = 0
containerView.layer.cornerRadius = 25
containerView.clipsToBounds = true
let toastLabel = UILabel()
toastLabel.textColor = .white
toastLabel.textAlignment = .center
toastLabel.font.withSize(12.0)
toastLabel.text = message
toastLabel.clipsToBounds = true
toastLabel.numberOfLines = 0
baseView.addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.leadingAnchor.constraint(greaterThanOrEqualTo: baseView.leadingAnchor, constant: 65).isActive = true
containerView.trailingAnchor.constraint(lessThanOrEqualTo: baseView.trailingAnchor, constant: -65).isActive = true
containerView.bottomAnchor.constraint(equalTo: baseView.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
containerView.centerXAnchor.constraint(equalTo: baseView.centerXAnchor).isActive = true
containerView.addSubview(toastLabel)
toastLabel.translatesAutoresizingMaskIntoConstraints = false
toastLabel.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 15).isActive = true
toastLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 15).isActive = true
toastLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -15).isActive = true
toastLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -15).isActive = true
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseIn, animations: {
containerView.alpha = 1
}, completion: { _ in
UIView.animate(withDuration: 0.5, delay: 1.5, options: .curveEaseOut, animations: {
containerView.alpha = 0
}, completion: { _ in
containerView.removeFromSuperview()
})
})
}
}
이것은 또 다른 방법입니다.
func showToast(viewController: UIViewController?, message: String) {
let alertDisapperTimeInSeconds = 3.0
let toastLabel = UILabel(frame: CGRect(x: self.view.frame.size.width/2 - 75, y: self.view.frame.size.height-100, width: 150, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.font = .systemFont(ofSize: 14)
toastLabel.textAlignment = .center;
toastLabel.text = message
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
viewController?.view.addSubview(toastLabel)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + alertDisapperTimeInSeconds) {
toastLabel.alpha = 0.0
toastLabel.removeFromSuperview()
}
}
기능에 접근하는 방법
self.showToast(viewController: self, message: "I am a Toast Message")
여기 제 해결책이 있습니다.누군가에게 도움이 되길 바랍니다.
class Toasty {
static let DELAY_SHORT = 2.0
static let DELAY_LONG = 4.0
static let BG_OPACITY: Double = 0.9
static let TOAST_HEIGHT = 20
static let TOAST_WIDTH = 24
static let TOAST_ADJUSTMENT = 96
static let FONT_SIZE = 16
enum Gravity {
case bottom
case center
case top
}
enum Delay {
case short
case long
}
static func showToast(_ message: String, _ txtColor: UIColor, _ bgColor: UIColor, _ position: Gravity, _ delay: Delay) {
guard let window = UIApplication.shared.keyWindow else {
return
}
let label = ToastLabel()
label.textColor = txtColor
label.backgroundColor = bgColor
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: CGFloat(FONT_SIZE))
label.alpha = 0
label.text = message
label.numberOfLines = 0
var vertical: CGFloat = 0
var size = label.intrinsicContentSize
var width = min(size.width, window.frame.width - 60)
if width != size.width {
vertical = 1000
label.textAlignment = .justified
}
label.textInsets = UIEdgeInsets(top: vertical, left: 15, bottom: vertical, right: 15)
size = label.intrinsicContentSize
width = min(size.width, window.frame.width - 60)
if (position == Gravity.bottom) {
label.frame = CGRect(x: CGFloat(TOAST_WIDTH), y: window.frame.height - CGFloat(TOAST_ADJUSTMENT), width: width, height: size.height + CGFloat(TOAST_HEIGHT))
} else if (position == Gravity.center) {
label.frame = CGRect(x: CGFloat(TOAST_WIDTH), y: window.frame.height / 2, width: width, height: size.height + CGFloat(TOAST_HEIGHT))
} else if (position == Gravity.top) {
label.frame = CGRect(x: CGFloat(TOAST_WIDTH), y: CGFloat(TOAST_ADJUSTMENT), width: width, height: size.height + CGFloat(TOAST_HEIGHT))
}
label.center.x = window.center.x
label.layer.cornerRadius = min(label.frame.height / 2, 32)
label.layer.masksToBounds = true
window.addSubview(label)
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseIn, animations: {
label.alpha = 1
}, completion: { _ in
UIView.animate(withDuration: 0.5, delay: delay == Delay.long ? DELAY_LONG : DELAY_SHORT, options: .curveEaseOut, animations: {
label.alpha = 0
}, completion: {_ in
label.removeFromSuperview()
})
})
}
class func regular(msg: String, position: Gravity, delay: Delay) {
showToast(msg, UIColor(.white), UIColor(.black.opacity(BG_OPACITY)), position, delay)
}
class func info(msg: String, position: Gravity, delay: Delay) {
showToast(String("\u{24D8}")+" "+msg, UIColor.white, UIColor(Color(red: 0/255, green: 100/255, blue: 225/255).opacity(BG_OPACITY)), position, delay)
}
class func alert(msg: String, position: Gravity, delay: Delay) {
showToast(String("\u{26A0}")+" "+msg, UIColor.black, UIColor(Color(red: 255/255, green: 175/255, blue: 0/255).opacity(BG_OPACITY)), position, delay)
}
class func success(msg: String, position: Gravity, delay: Delay) {
showToast(String("\u{2705}")+" "+msg, UIColor.white, UIColor(Color(red: 0/255, green: 150/255, blue: 0/255).opacity(BG_OPACITY)), position, delay)
}
class func error(msg: String, position: Gravity, delay: Delay) {//2757
showToast(String("\u{274C}")+" "+msg, UIColor.white, UIColor(Color(red: 175/255, green: 0/255, blue: 0/255).opacity(BG_OPACITY)), position, delay)
} }
class ToastyLabel: UILabel {
var textInsets = UIEdgeInsets.zero {
didSet { invalidateIntrinsicContentSize() }
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let insetRect = bounds.inset(by: textInsets)
let textRect = super.textRect(forBounds: insetRect, limitedToNumberOfLines: numberOfLines)
let invertedInsets = UIEdgeInsets(top: -textInsets.top, left: -textInsets.left, bottom: -textInsets.bottom, right: -textInsets.right)
return textRect.inset(by: invertedInsets)
}
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: textInsets))
} }
용도:
@State var increment = 0
ZStack {
Button(action: {
increment += 1
if increment == 1 {
Toasty.info(msg: "Info Toasty", position: .bottom, delay: .short)
} else if increment == 2 {
Toasty.alert(msg: "Alert Toasty", position: .top, delay: .short)
} else if increment == 3 {
Toasty.success(msg: "Success Toasty", position: .center, delay: .short)
} else if increment == 4 {
Toasty.error(msg: "Error Toasty", position: .bottom, delay: .short)
} else if increment == 5 {
Toasty.regular(msg: "Regular Toasty", position: .top, delay: .short)
increment = 0
}
}, label: {
Text("Show Toasty")
.padding(12)
.background(RoundedRectangle(cornerRadius: 8)
.stroke(Color.blue, lineWidth: 1))
})}
1단계: 사용자 정의 토스트 보기에 사용할 새 Swift 파일을 만듭니다.
class ToastView: UIView {
private var messageLabel: UILabel!
init(message: String) {
super.init(frame: CGRect.zero)
configureUI(message: message)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
private func configureUI(message: String) {
// Customize your toast view's appearance
backgroundColor = UIColor.black.withAlphaComponent(0.8)
layer.cornerRadius = 10
clipsToBounds = true
messageLabel = UILabel(frame: CGRect.zero)
messageLabel.text = message
messageLabel.textColor = UIColor.white
messageLabel.numberOfLines = 0
messageLabel.textAlignment = .center
messageLabel.font = UIFont.systemFont(ofSize: 15)
addSubview(messageLabel)
messageLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
messageLabel.topAnchor.constraint(equalTo: topAnchor, constant: 8),
messageLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 16),
messageLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -16),
messageLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8)
])
}
}
2단계: 토스트 메시지를 표시하려면 다음 확장자를 UIViewController에 추가합니다.
extension UIViewController {
func showToast(message: String, duration: TimeInterval = 2.0) {
let toastView = ToastView(message: message)
view.addSubview(toastView)
toastView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
toastView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
toastView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -50),
toastView.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 20),
toastView.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: -20)
])
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) {
UIView.animate(withDuration: 0.3, animations: {
toastView.alpha = 0
}, completion: { _ in
toastView.removeFromSuperview()
})
}
}
}
3단계: 이제 보기 컨트롤러에서 showTast 메서드를 사용하여 토스트 메시지를 표시할 수 있습니다.
// Example usage in your UIViewController:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Call the showToast method to display a toast message
showToast(message: "Hello, this is a toast message!")
}
}
Swift 4.2 매우 쉽고 놀라운 방법
let toastLabel = UILabel()
toastLabel.lineBreakMode = .byWordWrapping
toastLabel.numberOfLines = 0
toastLabel.text = "Type your message you want to show in toast"
toastLabel.sizeToFit()
//MARK Resize the Label Frame
toastLabel.frame = CGRect(x: toastLabel.frame.origin.x, y: toastLabel.frame.origin.y, width: toastLabel.frame.size.width + 40, height: toastLabel.frame.size.height + 40)
self.view.addSubview(toastLabel)
언급URL : https://stackoverflow.com/questions/31540375/how-to-create-a-toast-message-in-swift
'sourcecode' 카테고리의 다른 글
Angular 2가 모델을 로드/분해할 때까지 기다린 후 뷰/템플릿 렌더링 (0) | 2023.08.06 |
---|---|
사용자별 Oracle 설정 기본 구성표(세션 변경 안 함) (0) | 2023.08.06 |
CSS를 사용하면 다중 행의 오버플로 블록에 "..."를 사용합니다. (0) | 2023.08.06 |
phpMyAdmin 오류: mysqli_real_connect(): (HY000/1045):'pma'@'localhost' 사용자에 대한 액세스가 거부되었습니다(암호 사용: 아니요). (0) | 2023.08.06 |
혜성과 jQuery (0) | 2023.08.06 |