2016-08-23 25 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

iveはこれを試しましたが、うまく動作しません。ありがとう – Wazza

+0

原点を設定した後でself.view.layoutIfNeeded()を追加してみてください。 –

+0

は、hasntも移動させました。私はそれがなぜ動かないのか理解していない – Wazza

0

AutoLayoutを使用している場合は、次の手順を実行してください。

1)変更したいUILableの制約のコンセントを設定します。

2)次に、必要に応じてその制約の定数を変更します。 xPosOfLable.constant = X

0

あなたのラベルはストーリーボードで自動レイアウトで制約がある場合は、

titleLabel.translatesAutoresizingMaskIntoConstraints = YESを使用してframe.Tryを移動するには制約を無効にする必要があります。

+0

私はストーリーボードをまったく使っていません – Wazza

0

私はスナップキットのココアポッドを使用して制約を作成し、これらの制約を宣言する前にサブビューを追加することでこれを解決することができました。

ありがとうございました。

HERESに私は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) 
      } 



     } 
関連する問題