以UIView的UILabel为中心

时间:2017-11-26 14:07:01

标签: ios swift uiview

我目前正在尝试制作一个简单的弹出窗口,这里我有一个按钮,一旦它被点击。

它应该在其上方显示一个简单的视图,我实现了blue background,但我无法为其添加标签,并将label置于blue popup <的中心位置/ p>

let width = (sender as! UIButton).frame.width
let y = (sender as! UIButton).frame.origin.y
let x = (sender as! UIButton).frame.origin.x

myView = UIView(frame: CGRect(x: x, y: y - 30, width:width, height: 30))
myView.backgroundColor = UIColor.blue

let label = UILabel()
label.text = (sender as! UIButton).titleLabel?.text
label.font = UIFont(name:"avenirNext-Meduim",size:20)
label.center = self.myView.center
myview.addSubview(label)
self.view.addSubview(myView)

UIView.animate(withDuration: 0.1, delay: 0.1, options: [], animations: {

    self.myView.alpha = 0.0

}) { (finished: Bool) in

    self.myView.isHidden = true
}

1 个答案:

答案 0 :(得分:0)

let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50)) 让标签 = UILabel() label.frame = CGRect.init(x: headerView.center.x-60, y:headerView.center.y-15, width: 120, height: 30) label.textAlignment = .center label.textColor = #colorLiteral(红色:0.1290470958,绿色:0.2743584514,蓝色:0.6418049335,alpha:1) label.font = UIFont(name: "Roboto-Bold", size: 15) label.text = "11-12-2020" headerView.addSubview(标签) headerView.backgroundColor = #colorLiteral(红色:0.9967060685,绿色:0.9998621345,蓝色:0.9997505546,alpha:1)