【问题标题】:Having trouble getting UILabel to display on top of UIView无法让 UILabel 显示在 UIView 之上
【发布时间】:2018-03-08 04:02:23
【问题描述】:

我正在尝试创建一个自定义视图,该视图由 UIView 之上的 UILabel 组成。这个想法是作为一个小的弹出消息,标签基本上与视图大小相同。但是,每当我尝试将此自定义视图(由屏幕截图中屏幕底部的小灰色框表示)添加到另一个视图时,我只能显示 UIView/深灰色背景,但不能显示标签。有人可以指出我正确的方向吗,因为我对为什么下面的代码不起作用感到有些困惑?谢谢。

编辑:我可能应该指出,如果我将 .zero 替换为 CGRect(x: 0, y: 0, height: 100, width: 50),我可以让标签以某种形式显示,但我会考虑到约束,认为将其设置为 .zero 就可以了吗?

import UIKit

class PopUp: UIView {
    var label: UILabel!
    var labelText: String!

    init(frame: CGRect, text: String) {
        super.init(frame: frame)

        self.labelText = text

        self.backgroundColor = .darkGray        

        self.label = UILabel(frame: .zero)
        self.label.textColor = UIColor.white
        self.label.text = self.labelText
        self.label.font = UIFont(name: "Times New Roman", size: 14.0)
        self.translatesAutoresizingMaskIntoConstraints = false // this should be "self.label.translatesAutoresizingMaskIntoConstraints"

        self.addSubview(self.label)
        self.bringSubview(toFront: self.label)

        NSLayoutConstraint.activate([
            self.label.topAnchor.constraint(equalTo: self.topAnchor),
            self.label.bottomAnchor.constraint(equalTo: self.bottomAnchor),
            self.label.leadingAnchor.constraint(equalTo: self.leadingAnchor),
            self.label.trailingAnchor.constraint(equalTo: self.trailingAnchor)
        ])
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }
}

【问题讨论】:

  • 使用视图控制器而不是 UIView 子类,其呈现样式为 overcurrentcontext
  • 我已回滚您的编辑。如果您解决了问题,请发布答案说明如何解决;这个问题应该只是一个问题。 (接受你自己的答案很好。)
  • 添加了答案。看来我必须等两天才能接受自己的答案,所以我会确保这样做。

标签: ios swift uilabel constraints


【解决方案1】:

对不起,伙计们,我是个白痴。我盯着这段代码看了几个小时,不明白为什么事情不正常。我最终意识到我的代码中有一个错字。修复错字似乎已经成功了。感谢大家的帮助!

原文:

self.translatesAutoresizingMaskIntoConstraints

固定:

self.label.translatesAutoresizingMaskIntoConstraints

【讨论】:

    【解决方案2】:

    如果您想将消息显示为弹出窗口,为什么不在 UIViewController 扩展中编写弹出窗口并在控制器中调用它?

    extension UIViewController {
         func showPopup(text: String) {
    
              let popupView: UIView = {
                  let view = UIView()
                  view.backgroundColor = .darkGray
                  view.translatesAutoresizingMaskIntoConstraints = false
                  return view
              }()
    
              let textLabel: UILabel = {
                  let label = UILabel()
                  label.text = self.text
                  label.font = UIFont(name: "Times New Roman", size: 14.0)
                  label.textColor = UIColor.white
                  label.translatesAutoresizingMaskIntoConstraints = false
                  return label
              }()
              self.view.addSubview(popupView) 
              // add anchors to your popupView relative to your viewController here
    
              self.popupView.addSubView(textLabel)
              self.textLabel.topAnchor.constraint(equalTo: popupView.topAnchor).isActive = true
              self.textLabel.bottomAnchor.constraint(equalTo: popupView.bottomAnchor).isActive = true
              self.textLabel.leftAnchor.constraint(equalTo: popupView.leftAnchor).isActive = true
              self.textLabel.rightAnchor.constraint(equalTo: popupView.rightAnchor).isActive = true
        }
    }
    

    并在视图控制器中调用该函数:

    self.showPopup(text: "My popup message")
    

    【讨论】:

    • 感谢 leedex。我将尝试重构这个。
    【解决方案3】:

    初始化后给标签设置文本

    self.label = UILabel(frame: .zero)
    self.labelText = text
    

    您在初始化之前设置文本。

    【讨论】:

    • 在这种情况下我很幸运,因为这并没有导致错误,但我编辑了代码以反映您的观点。谢谢!
    【解决方案4】:

    问题:

    我对你的脚本做了一些测试,从头开始重写,发现了你的错误。

    问题是,您没有导入“Times New Roman”字体。为此,您需要右键单击黄色文件夹,按 Command + Shift + G,然后输入:

    /System/Library/Fonts/
    

    然后,向下滚动直到看到“Times”字体,然后将其上传到 Swift。

    我还建议将 "Times New Roman" 的部分更改为 "Times"

    还是不行?

    如果仍然无法正常工作,则说明您的代码有问题。我能够从头开始重写你的脚本,它对我有用。如果仍然无法正常工作,请尝试使用以下代码:

    import UIKit
    class ViewController: UIViewController {
        var popupLabel = UILabel()
        var popupBox = UIView()
        override func viewDidLoad() {
            view.addSubview(popupBox)
            popupBox.addSubview(popupLabel)
            popupBox.isHidden = true
            popupLabel.isHidden = true
            showPopup(CGPoint(x: 250 /* Change X Position */, y: 125 /* Change Y Position */), CGSize(width: 250 /* Change Width */, height: 100 /* Change Height */), "Blah" /* Change displayed text */)
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
    func showPopup(_ rectPosition: CGPoint, _ rectSize: CGSize, _ popupDisplay: String) {
        popupBox.isHidden = false
        popupLabel.isHidden = false
        popupLabel.textColor = UIColor.white
        popupLabel.font = UIFont(name: "Times New Roman", size: 14.0)
        self.popupLabel.text = popupDisplay
        popupBox.frame.size = rectSize
        popupBox.center = rectPosition
        popupBox.backgroundColor = UIColor.darkGray
        popupLabel.frame.size = popupBox.frame.size
        popupLabel.textAlignment = .center
        popupLabel.center = popupBox.center
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    }
    

    如果您打算使用此代码,请将其放在 ViewController.swift 文件中,并考虑删除其他类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多