我正在創建一個自來水戰爭遊戲,並將自定義視圖添加到我的應用程序,redView和blueView。這兩個視圖都佔據了屏幕的一半。我試圖在兩個視圖中的每一個上定位一個標籤,以便跟蹤分數(標籤稱爲「topviewLabel」和「bottomViewLabel」),特別是在中心,但標籤顯示在其各自視圖的左上角並不會正確居中。我以編程方式創建視圖並設置它們的約束,然後將標籤添加到redView或blueView,我以編程方式完成所有這些操作。我會留下下面的代碼,讓我知道我做錯了什麼。無法將我的UILabel居中在我的自定義視圖上
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let redView = UIView()
redView.backgroundColor = UIColor(red: 206/255, green: 0/255, blue: 0/255, alpha: 1)
redView.isMultipleTouchEnabled = true
redView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(redView)
let redviewConstraints: [NSLayoutConstraint] = [
NSLayoutConstraint(item: redView, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0),
NSLayoutConstraint(item: redView, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1, constant: 0),
NSLayoutConstraint(item: redView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 0),
NSLayoutConstraint(item: redView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: view.frame.height/2)
]
view.addConstraints(redviewConstraints)
let blueView = UIView()
blueView.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 99/255, alpha: 1)
blueView.isMultipleTouchEnabled = true
blueView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(blueView)
let blueViewConstraints: [NSLayoutConstraint] = [
NSLayoutConstraint(item: blueView, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: 0),
NSLayoutConstraint(item: blueView, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1, constant: 0),
NSLayoutConstraint(item: blueView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 0),
NSLayoutConstraint(item: blueView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: view.frame.height/2)
]
view.addConstraints(blueViewConstraints)
let topViewLabel = UILabel()
topViewLabel.frame = CGRect(x: redView.frame.size.width/2, y: redView.frame.size.height/2, width: 80, height: 80)
topViewLabel.text = "30"
topViewLabel.textAlignment = .center
topViewLabel.font.withSize(30)
topViewLabel.textColor = .white
topViewLabel.backgroundColor = UIColor.blue
topViewLabel.center = redView.center
redView.addSubview(topViewLabel)
redView.bringSubview(toFront: topViewLabel)
let bottomViewLabel = UILabel()
bottomViewLabel.frame = CGRect(x: blueView.frame.width/2, y: blueView.frame.height/2, width: 80, height: 80)
bottomViewLabel.center = blueView.center
bottomViewLabel.text = "20"
bottomViewLabel.backgroundColor = UIColor.red
blueView.addSubview(bottomViewLabel)
blueView.bringSubview(toFront: bottomViewLabel)
}
我在控制檯窗口的UIView屬性translatesAutoresizingMaskIntoConstraints收到此) ( 「」, 「」, 「」 ) 威爾嘗試通過中斷約束來恢復
jumpman007
當我啓動應用程序時,標籤居中但在紅色視圖底部中途切斷 – jumpman007
當您從代碼添加約束時,通常需要將受約束視圖的translatesAutoresizingMaskIntoConstraints屬性設置爲false,如同對紅色和藍色視圖所做的那樣。因此,添加'topViewLabel.translatesAutoresizingMaskIntoConstraints = false'應該有幫助 – Kitmap