我正在尝试将标签放置在视图的中心。但是,它始终偏离中心。为什么是这样?
这是代码:
lazy var readyLabel = UILabel(frame: CGRect(x: screenWidth*0.5 - screenWidth*0.3, y: screenHeight*0.5, width: screenWidth*0.6, height: screenHeight*0.1 ))
readyLabel.textAlignment = .center
readyLabel.text = "3"
readyLabel.textColor = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1)
readyLabel.alpha = 0
readyLabel.font = UIFont(name: "Futura-Bold", size: (75.0/375)*screenWidth)
readyLabel.sizeToFit()
self.view.addSubview(readyLabel)
答案 0 :(得分:0)
label.center = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.height / 2)
或
child.center = parent.convert(parent.center, from:parent.superview)
答案 1 :(得分:0)
在最后一行的之后添加此:
readyLabel.center = self.view.convertPoint(self.view.center, from: self.view.superview)
答案 2 :(得分:0)
只需删除该行:
readyLabel.sizeToFit()