Saltar al contenido

ejemplo de código de mensaje ios swift toast

Hola usuario de nuestra página, hemos encontrado la solución a tu búsqueda, desplázate y la encontrarás aquí.

Ejemplo: brindis de alerta rápida

/////////////////////////////////////////////////// Tested//											/////////				implementation				/////////				Part 2						/////////											////////////////////////////////////////////////////////////here we call Toast Bubble it inside our button "just copy it in your controller"@IBOutletweakvar txtPassword:UITextField!@IBOutletweakvar txtUserName:UITextField!//MARK: Login Action@IBActionfuncbtnLoginPressed(_ sender:UIButton)if txtUserName.text ==""//Call UIView extension methodself.view.showToast(toastMessage:"Please Enter Username,beacuse it is necessary to add For getting logged in.", duration:1.1)elseif txtPassword.text ==""self.view.showToast(toastMessage:"Please Enter Password.", duration:1.1)else//Do login/////////////////////////////////////////////////////											/////////				create Toast				/////////				Part 1						/////////											////////////////////////////////////////////////////////////This is Extention of toast "it will stay outside controller"importUIKit//MARK: Add Toast method function in UIView Extension so can use in whole project.extensionUIViewfuncshowToast(toastMessage:String,duration:CGFloat)//View to blur bg and stopping user interactionlet bgView =UIView(frame:self.frame)
        bgView.backgroundColor =UIColor(red:CGFloat(255.0/255.0), green:CGFloat(255.0/255.0), blue:CGFloat(255.0/255.0), alpha:CGFloat(0.6))
        bgView.tag =555//Label For showing toast textlet lblMessage =UILabel()
        lblMessage.numberOfLines =0
        lblMessage.lineBreakMode =.byWordWrapping
        lblMessage.textColor =.white
        lblMessage.backgroundColor =.black
        lblMessage.textAlignment =.center
        lblMessage.font =UIFont.init(name:"Helvetica Neue", size:17)
        lblMessage.text = toastMessage
        
        //calculating toast label frame as per message contentlet maxSizeTitle :CGSize=CGSize(width:self.bounds.size.width-16, height:self.bounds.size.height)var expectedSizeTitle :CGSize= lblMessage.sizeThatFits(maxSizeTitle)// UILabel can return a size larger than the max size when the number of lines is 1
        expectedSizeTitle =CGSize(width:maxSizeTitle.width.getminimum(value2:expectedSizeTitle.width), height: maxSizeTitle.height.getminimum(value2:expectedSizeTitle.height))
        lblMessage.frame =CGRect(x:((self.bounds.size.width)/2)-((expectedSizeTitle.width+16)/2), y:(self.bounds.size.height/2)-((expectedSizeTitle.height+16)/2), width: expectedSizeTitle.width+16, height: expectedSizeTitle.height+16)
        lblMessage.layer.cornerRadius =8
        lblMessage.layer.masksToBounds =true
        lblMessage.padding =UIEdgeInsets(top:4,left:4, bottom:4,right:4)
        bgView.addSubview(lblMessage)self.addSubview(bgView)
        lblMessage.alpha =0UIView.animateKeyframes(withDuration:TimeInterval(duration), delay:0, options:[], animations:
            lblMessage.alpha =1, completion:
            sucess inUIView.animate(withDuration:TimeInterval(duration), delay:8, options:[], animations:
                lblMessage.alpha =0
                bgView.alpha =0)
            bgView.removeFromSuperview())extensionCGFloatfuncgetminimum(value2:CGFloat)->CGFloatifself< value2
        returnselfelsereturn value2
        //MARK: Extension on UILabel for adding insets - for adding padding in top, bottom, right, left.extensionUILabelprivatestructAssociatedKeysstaticvar padding =UIEdgeInsets()var padding:UIEdgeInsets?getreturnobjc_getAssociatedObject(self,&AssociatedKeys.padding)as?UIEdgeInsetssetiflet newValue = newValue objc_setAssociatedObject(self,&AssociatedKeys.padding, newValue asUIEdgeInsets?, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)override open funcdraw(_ rect:CGRect)iflet insets = padding self.drawText(in: rect.inset(by: insets))elseself.drawText(in: rect)override open var intrinsicContentSize:CGSizegetvar contentSize =super.intrinsicContentSize
            iflet insets = padding 
                contentSize.height += insets.top + insets.bottom
                contentSize.width += insets.left+ insets.rightreturn contentSize
        

Tienes la opción de sostener nuestra publicación exponiendo un comentario o puntuándolo te damos la bienvenida.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *