【问题标题】:Swift 3 - Add Text to UIView Pop upSwift 3 - 向 UIView 弹出添加文本
【发布时间】:2018-02-18 21:46:43
【问题描述】:

我想显示一个包含一些固定文本的弹出窗口。

我为弹出窗口找到了这个。

    // Define a view
    var popup:UIView!
    func showAlert() {
    // customise your view
    popup = UIView(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
    popup.backgroundColor = UIColor.redColor

    // show on screen
    self.view.addSubview(popup)

    // set the timer
    Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.dismissAlert), userInfo: nil, repeats: false)
  }

  func dismissAlert(){
    if popup != nil { // Dismiss the view from here
      popup.removeFromSuperview()
    }
  }

我似乎无法在 showAlert() 中添加标签并显示它。

我尝试在 showAlert 中调用一个单独的函数来获取标签文本,该函数有效,但不会关闭。

如何在 showAlert 函数本身的弹出窗口中添加文本/字符串/标签?我想使用 UIView 本身,而不是 AlertController。

【问题讨论】:

  • 您在哪里尝试添加标签?

标签: ios swift text swift3 popup


【解决方案1】:

你可以试试

popup = UIView(frame: CGRect(x: 100, y: 200, width: 200, height: 200))

let lb = UILabel(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
lb.text="anything"
popup.backgroundColor = UIColor.redColor

// show on screen
self.view.addSubview(popup)
popup.addSubview(lb)
lb.center = popup.center

【讨论】:

  • 我正要删除这个问题。我得到了相同的答案并让它发挥作用。还是谢谢你:)
  • @KanavBatra 如果可行,那么您应该接受 Sh_Khan 的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-30
  • 2015-07-16
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多