swift - 尽管 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)
解决方案
好吧,我放弃了,只是在这里阅读:
在顶部添加 UILabel 作为 UITextField 的子视图
因此切换到使用:
https://github.com/teodorpatras/EasyTipView
MIT 许可证,可以正常工作。
恕我直言,Apple 没有提供诸如本机工具提示等如此简单的东西,这完全令人沮丧。
推荐阅读
- python - 从 PDF 中提取声音注释
- javafx - 如何在方法中启用 Platform.runlater
- python - python中tkinter中的'width'和'padx'有什么区别?
- css - Chrome DevTools 在使用 Inspect Element 编辑 CSS 后将 CSS Link Files 更改为“Constructed Stylesheet”,如何解决?
- elasticsearch - 如何在 Kibana 中将自定义字段视为标准类型而不是未知类型?
- javascript - 在源卡上条纹 V3 持卡人姓名
- python-3.x - 在执行下一个代码块之前,如何告诉我的 python 脚本等待“另存为”对话框在 chrome 中弹出?
- python - Python - 在循环中将数据导出到 CSV 仅保存最后一行输出
- ios - Flutter:在权限处理程序中添加IOS上的位置权限
- android - 如何在登录 Android 时更新 Firebase 用户个人资料?