我有一個帶標籤的容器視圖,容器視圖以超級視圖大小的一半以編程方式添加到超視圖的下半部分。我設置的大小是這樣的:Swift:如何找出容器視圖的大小並以編程方式在中心放置一個標籤?
testView.frame.size = CGSize(width: view.bounds.width, height: view.bounds.height/2)
(橙色廣場是containerView和多數民衆贊成在我想打標籤 - 容器視圖中心)
這樣它就是superview的一半大小。 現在我想通過編程的方式將標籤放在容器視圖的正中間。實際上它應該用CGAffineTransform動畫並移動到中心,但爲了我的問題,它並不重要。
我已經嘗試了幾件事情,但即使像self.testView.midX/midY這樣的簡單解決方案也不起作用。它不居中,或者標籤放置在容器外的某處。
我試圖讓意見寬度和高度:
let timerViewWidth = self.timerView.bounds.width
let timerViewHeight = self.timerView.bounds.height
和除以2,既要得到testView的正中心,但是當我使用這些值的標籤也被放在別的地方。
如何以編程方式將標籤居中放在容器視圖中?
你應該看看自動佈局和約束,但如果你真的不想......你可以使用'testView.center = view.center' – DonMag