首页 > 解决方案 > 尽管 zPosition = 1,但无法强制子视图位于最前面

问题描述

我正在尝试将标签放在前面,但没有运气:

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);
    }
    

如果标签 isHidden 设置为 false,则标签出现,但不在最前面: 在此处输入图像描述

我试过:

helpLabel.layer.zPosition = 1;
self.bringSubviewToFront(helpLabel);
UIApplication.shared.keyWindow!.bringSubviewToFront(helpLabel)

标签: swiftxcodeuikit

解决方案


好吧,我放弃了,只是在这里阅读:

在顶部添加 UILabel 作为 UITextField 的子视图

因此切换到使用:

https://github.com/teodorpatras/EasyTipView

MIT 许可证,可以正常工作。

恕我直言,Apple 没有提供诸如本机工具提示等如此简单的东西,这完全令人沮丧。


推荐阅读