gpt4 book ai didi

ios - 如何在 View Swift 顶部添加标签

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:02 26 4
gpt4 key购买 nike

这是我正在尝试做的事情:

ScreeShot

截图来自iPhone:

Screen Shot

代码如下:

//Show HighLightView
let highLightView = UIView(frame: CGRect(x: self.minXHighLightView!, y: 0, width: self.maxXHighLightView!-self.minXHighLightView!, height: self.soundWaveView.frame.height))
highLightView.backgroundColor = UIColor(displayP3Red: 89/256, green: 206/256, blue: 249/256, alpha: 0.4)
self.soundWaveView.addSubview(highLightView)

//Show HighLight Lable
let highLightLabelCount = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
highLightLabelCount.textColor = .black
highLightLabelCount.font = UIFont(name: "Lato-Bold", size: 20)
highLightView.addSubview(highLightLabelCount)

问题:如何设置左上角的Frame或constraints标签设置?

如有任何帮助,我们将不胜感激。

提前致谢。

最佳答案

您的约束应该以 x,y 为中心,而不是左上角

//Show HighLightView
let highLightView = UIView(frame: CGRect(x: self.minXHighLightView!, y: 0, width: self.maxXHighLightView!-self.minXHighLightView!, height: self.soundWaveView.frame.height))
highLightView.backgroundColor = UIColor(displayP3Red: 89/256, green: 206/256, blue: 249/256, alpha: 0.4)
self.soundWaveView.addSubview(highLightView)

//Show HighLight Lable
let highLightLabelCount = UILabel()
highLightLabelCount.textColor = .black
highLightLabelCount.font = UIFont(name: "Lato-Bold", size: 20)
highLightView.addSubview(highLightLabelCount)
highLightLabelCount.translatesAutoresizingMaskIntoConstraints = false

self.highLightView.textAlignment = .center
NSLayoutConstraint.activate([
self.highLightLabelCount.centerXAnchor.constraint(equalTo: self.highLightView.centerXAnchor),
self.highLightLabelCount.centerYAnchor.constraint(equalTo: self.highLightView.centerYAnchor)
])

关于ios - 如何在 View Swift 顶部添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56687745/

26 4 0