0

I'm trying to bring a label into front, but with no luck:

class UITextFieldCustom : UITextField, UITextFieldDelegate {
    
    public var valueSuccess = true;
    public var textValue  = "";
    public var keyboardToolBar : UIToolbar!;
    public var helpLabel: UILabel = UILabel();
    
// MARK: - init
    
    init(frame: CGRect, size: CGFloat) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layer.borderColor = UIColor.gray.cgColor
        
        let f = self.bounds;
        helpLabel.frame = CGRect(x: f.origin.x - 20, y: f.origin.y - 10, width: 75, height: 30);
        helpLabel.backgroundColor = UIColor.systemBlue;
        helpLabel.textColor = UIColor.white;
        helpLabel.layer.borderColor = UIColor.red.cgColor;
        helpLabel.layer.borderWidth = 0.5;
        helpLabel.layer.masksToBounds = true;
        helpLabel.layer.cornerRadius = 3;
        helpLabel.layer.zPosition = 1;
        helpLabel.isHidden = true;
        helpLabel.setMargins(10);
        self.addSubview(helpLabel);
    }
    

If the label isHidden is set to false, the label appears, but not in frontmost position: enter image description here

I've tried with:

helpLabel.layer.zPosition = 1;
self.bringSubviewToFront(helpLabel);
UIApplication.shared.keyWindow!.bringSubviewToFront(helpLabel)
frameworker
  • 298
  • 1
  • 3
  • 16

1 Answers1

0

Okay, I give up and just read here:

Add UILabel as subview of UITextField on top

and therefore switched to use:

https://github.com/teodorpatras/EasyTipView

MIT license, works without problems.

IMHO it's totally depressive that Apple doesn't provide such simple things like native tooltips etc.

frameworker
  • 298
  • 1
  • 3
  • 16