2016-08-23 81 views
0

我無法改變UILabel的位置。我可以改變字體顏色和背景等,但它的位置似乎並不移動,不管我嘗試什麼。任何幫助,將不勝感激。我也沒有使用故事板。以編程方式移動UILabel不起作用

我對此很新,所以我可能錯過了一些非常明顯的東西。我搜索了一下,並嘗試了我認爲應用但沒有任何運氣的任何東西。

視圖創建工具:

import UIKit 

class StandMapView: UIView { 

    var titleLabel: UILabel = UILabel() 
    var standMapImage: UIImageView = UIImageView() 
    var hotspotImage: UIImageView = UIImageView() 
    var hotspotTitleLabelArray: [UILabel] = [] 
    var hotspotTextArray: [UITextView] = [] 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     setupView() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    func bind(standMap: StandMap, hotspots: [Hotspot]) { 
     titleLabel.text = standMap.title 
     standMapImage.image = UIImage(named: standMap.mapImage) 
     hotspotImage.image = UIImage(named:standMap.hotspotImage) 
     for hotspot in hotspots { 
      let hotspotTitle = UILabel() 
      let hotspotText = UITextView() 
      hotspotTitle.text = hotspot.title 
      hotspotText.text = hotspot.text 
      hotspotTitleLabelArray.append(hotspotTitle) 
      hotspotTextArray.append(hotspotText) 
     } 
    } 

    private func setupView() { 

     let screenWidth = UIScreen.mainScreen().bounds.width 
     let screenHeight = UIScreen.mainScreen().bounds.height 
     self.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight) 

     titleLabel.translatesAutoresizingMaskIntoConstraints = false 
     standMapImage.translatesAutoresizingMaskIntoConstraints = false 
     hotspotImage.translatesAutoresizingMaskIntoConstraints = false 

     self.backgroundColor = UIColor.blackColor() 

     titleLabel.sizeToFit() 

     titleLabel.frame = CGRect(x: screenWidth/2, y: 30, width: 0, height: 0) 
     titleLabel.textAlignment = .Center 
     titleLabel.numberOfLines = 0 
     titleLabel.adjustsFontSizeToFitWidth = true 
     titleLabel.textColor = UIColor.whiteColor() 

     addSubview(titleLabel) 

    } 

} 

視圖控制器:

import UIKit 

    class StandMapViewController: UIViewController { 

     var standMap: StandMap! 
     var hotspots: [Hotspot] = [] 

     override func viewDidLoad() { 
      super.viewDidLoad() 

      Hotspot.all { hotspot in 
       hotspot.forEach(self.assignHotspotVariable) 
      } 

      StandMap.build {standMap in 
       standMap.forEach(self.assignStandMapVariable) 
      } 

      viewForStandMap(standMap, hotspots: hotspots) 

     } 

     private func assignStandMapVariable(standMap: StandMap) { 
      self.standMap = standMap 
     } 

     private func assignHotspotVariable(hotspot: Hotspot) { 
      hotspots.append(hotspot) 
     } 

     private func viewForStandMap(standMap: StandMap, hotspots: [Hotspot]) { 
      let standMapView = StandMapView(frame: CGRectZero) 
      standMapView.bind(standMap, hotspots: hotspots) 
      view.addSubview(standMapView) 
     } 
    } 
+0

如果要添加標籤,你的看法,你也應該設置它在它的幀沒有采取全屏幕尺寸 –

+1

請張貼更少的代碼;只有那個重要。 –

回答

0

如果要更改標籤的位置,你需要改變原點x和y

titleLabel.frame.origin.x = 0.0 // put your value 
titleLabel.frame.origin.y = 0.0 // put your value 
self.view.layoutIfNeeded() 
+0

我試過這個,但它不起作用。雖然 – Wazza

+0

嘗試添加self.view.layoutIfNeeded()後設置原始 –

+0

還沒有讓它移動。我不知道爲什麼它不會移動 – Wazza

0

如果您使用AutoLayout,請執行以下操作:

1)爲要更改的UILable的約束設置出口。

2)然後根據需要改變該約束的常量,例如, xPosOfLable.constant = X

0

如果您的標籤與在故事板自動佈局的限制,你必須禁用限制使用

titleLabel.translatesAutoresizingMaskIntoConstraints = YES移動frame.Try;

+0

即時消息不會使用storyboard – Wazza

0

我設法解決這個問題,使用snapkit cocoa pod做出約束,然後在聲明這些約束之前添加子視圖。

感謝大家的幫助。

繼承人的變化我對setupView函數功能提出:

 private func setupView() { 

      titleLabel.translatesAutoresizingMaskIntoConstraints = false 
      standMapImage.translatesAutoresizingMaskIntoConstraints = false 
      hotspotImage.translatesAutoresizingMaskIntoConstraints = false 

      self.backgroundColor = UIColor.blackColor() 
      titleLabel.textColor = UIColor.whiteColor() 
      titleLabel.textAlignment = .Center 
      titleLabel.numberOfLines = 0 
      titleLabel.adjustsFontSizeToFitWidth = true 
      addSubview(titleLabel) 

      titleLabel.snp_makeConstraints { make in 
       make.topMargin.equalTo(snp_topMargin).multipliedBy(60) 
       make.centerX.equalTo(snp_centerX) 
      } 



     } 
相關問題