2017-04-20 32 views
0

我正在創建一個自來水戰爭遊戲,並將自定義視圖添加到我的應用程序,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) 

} 

回答

0

作爲保利斯說,在viewDidLoad你的子視圖沒有設置其框架。所以,當,例如,您嘗試設置您的標籤框架

topViewLabel.frame = CGRect(x: redView.frame.size.width/2, y: redView.frame.size.height/2, width: 80, height: 80) 

redview.frame具有高度和寬度0

這些參數將只在layoutSubviews正確計算。

因此,您可以在viewDidLayoutSubviews()方法 或(我會這樣做)中使用約束來設置標籤幀。像

let topViewLabelConstraints: [NSLayoutConstraint] = [ 
    NSLayoutConstraint(item: topViewLabel, attribute: .centerX, relatedBy: .equal, toItem: redView, attribute: .centerX, multiplier: 1, constant: 0), 
    NSLayoutConstraint(item: topViewLabel, attribute: .centerY, relatedBy: .equal, toItem: redView, attribute: .centerY, multiplier: 1, constant: 0) 
] 
redView.addConstraints(topViewLabelConstraints) 

應該做的伎倆。

+0

我在控制檯窗口的UIView屬性translatesAutoresizingMaskIntoConstraints收到此) ( 「」, 「」, 「」 ) 威爾嘗試通過中斷約束來恢復 jumpman007

+0

當我啓動應用程序時,標籤居中但在紅色視圖底部中途切斷 – jumpman007

+0

當您從代碼添加約束時,通常需要將受約束視圖的translatesAutoresizingMaskIntoConstraints屬性設置爲false,如同對紅色和藍色視圖所做的那樣。因此,添加'topViewLabel.translatesAutoresizingMaskIntoConstraints = false'應該有幫助 – Kitmap

0

一個UIView沒有添加約束後的正確的幀大小的權利,你需要添加:

blueView.setNeedsLayout() 
blueView.layoutIfNeeded() 

現在blueView的框架設置。

+0

它仍然沒有解決這個問題,任何其他的想法? – jumpman007

相關問題