[SWIFT] Extension UITextField, problème de z-index ?

2»

Réponses

  • Par contre, vu que tout ton textfield a un bord noir, la carré noir de tes leftView et rightView ne sert plus rien, tu peux le virer.

    Tout ça tu peux le dégager :

    let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 45))
    view.backgroundColor = .white
    view.clipsToBounds = true
    view.layer.cornerRadius = 5
    view.layer.borderWidth = 0.5
    view.layer.borderColor = colorBorder.cgColor
    mainView.addSubview(view)
    

    et remplacer cette ligne :

    view.addSubview(imageView)
    

    par :

    mainView.addSubview(imageView)
    
  • PyrohPyroh Membre

    Je suis rentré tard et j'ai plus envie de regarder L'Empire contre attaque que de finir la playground 😃
    En attendant voilà un bout de code que tu peux analyser :

    let direction: Direction = .right
    let view = UIView(frame: CGRect(x: 25, y: 25, width: 50, height: 45))
    presentingView.addSubview(view)
    
    //: ## Création du layer main
    let mainLayer = view.layer
    mainLayer.name = "main"
    mainLayer.masksToBounds = true
    mainLayer.backgroundColor = UIColor.white.cgColor
    mainLayer.cornerRadius = 5
    mainLayer.fillMode = kCAFillModeForwards
    
    //: ## Création du layer stroke
    let strokeLayer = CALayer()
    strokeLayer.name = "stroke"
    strokeLayer.bounds = CGRect(x: 0, y: 0, width: 50, height: 45)
    strokeLayer.position = CGPoint(x: 25, y: 22.5)
    strokeLayer.contentsGravity = kCAGravityCenter
    strokeLayer.backgroundColor = nil
    strokeLayer.cornerRadius = 5
    strokeLayer.borderWidth = 0.5
    strokeLayer.borderColor = UIColor.black.cgColor
    strokeLayer.fillMode = kCAFillModeForwards
    mainLayer.addSublayer(strokeLayer)
    
    
    let separatorLayer = CALayer()
    separatorLayer.name = "separator"
    separatorLayer.bounds = CGRect(x: 0, y: 0, width: 5, height: 45)
    separatorLayer.position = CGPoint(x: 0, y: 22.5)
    separatorLayer.anchorPoint = CGPoint(x: 0, y: 0.5)
    separatorLayer.contentsGravity = kCAGravityCenter
    separatorLayer.backgroundColor = UIColor.orange.cgColor
    separatorLayer.maskedCorners = CACornerMask()
    separatorLayer.fillMode = kCAFillModeForwards
    switch direction {
    case .left:
        separatorLayer.anchorPoint = CGPoint(x: 0, y: 0.5)
        separatorLayer.position = CGPoint(x: mainLayer.bounds.minX, y: mainLayer.bounds.midY)
    case .right:
        separatorLayer.anchorPoint = CGPoint(x: 1, y: 0.5)
        separatorLayer.position = CGPoint(x: mainLayer.bounds.maxX, y: mainLayer.bounds.midY)
    }
    mainLayer.addSublayer(separatorLayer)
    
    let imageLayer = CALayer()
    imageLayer.name = "image"
    imageLayer.bounds = CGRect(x: 0, y: 0, width: 24, height: 24)
    imageLayer.position = CGPoint(x: 25, y: 22.5)
    imageLayer.contents = UIImage(named: "user.png")?.cgImage
    imageLayer.contentsGravity = kCAGravityResizeAspect
    imageLayer.contentsScale = mainLayer.contentsScale
    imageLayer.backgroundColor = nil
    imageLayer.fillMode = kCAFillModeForwards
    mainLayer.addSublayer(imageLayer)
    
  • JérémyJérémy Membre
    mai 2018 modifié #34

    Good job @Pyroh ! ;)

    Dans le mesure où il sette une couleur noire sur l’ensemble des bords du TextField, moi j’aurais viré la partie ci-dessous. Mais sinon c’est une bonne solution que tu devrais suivre @Insou.

    //: ## Création du layer stroke
    let strokeLayer = CALayer()
    strokeLayer.name = "stroke"
    strokeLayer.bounds = CGRect(x: 0, y: 0, width: 50, height: 45)
    strokeLayer.position = CGPoint(x: 25, y: 22.5)
    strokeLayer.contentsGravity = kCAGravityCenter
    strokeLayer.backgroundColor = nil
    strokeLayer.cornerRadius = 5
    strokeLayer.borderWidth = 0.5
    strokeLayer.borderColor = UIColor.black.cgColor
    strokeLayer.fillMode = kCAFillModeForwards
    mainLayer.addSublayer(strokeLayer)
    
  • Bonjour,

    Question bête probablement mais comment tester cela dans un playground ?

  • PyrohPyroh Membre

    Voilà je viens de finir la playground qui j'ai attaché à ce message.
    La premiere page montre le principe de base. La seconde ajoute 2/3 trucs sympa avec des animations.

    Pour les deux il faut afficher la live view pour en profiter.

  • InsouInsou Membre

    De retour.. je télécharge ça et je regarde ce que ça donne ^^

    En tout cas, merci à vous 2 :)

Connectez-vous ou Inscrivez-vous pour répondre.