2016-06-13 9 views
0

こんにちは、私はレイアウトについて質問があります。awakeFromNib()がシミュレーションメトリックで動作しません

私はストーリーボードを使用してレイアウトを設定します。

I had set on Size of iPhone 4.7-inch

そして私は、ストーリーボードを経由してUIコンポーネントを設定していました。

iPhone5またはiPhone 6 Plusをサポートするために、このようなコードを書いています。

enter//1. called awakeFromNib() 

@IBOutlet weak var profileImageView: UIImageView! 

override func awakeFromNib() { 

    super.awakeFromNib() 

    //alignment 
    let width = UIScreen.mainScreen().bounds.width 

    //ImageView Alignment 
    profileImageView.frame = CGRectMake(width/16, width/16, width/4 , width/4) 

    //Text Alignment 
    postsTextLabel.center = CGPointMake(postsTitleTextLabel.center.x, postsTitleTextLabel.center.y + 50) 


} 

// 2。このコードはiPhone6上で動作していないのはなぜ他の方法..ここ

//alignment 
     usernameButton.translatesAutoresizingMaskIntoConstraints = false 
     profileImagevView.translatesAutoresizingMaskIntoConstraints = false 
     commentLabel.translatesAutoresizingMaskIntoConstraints = false 
     dateLabel.translatesAutoresizingMaskIntoConstraints = false 

    //constraints 
    //Vertical 
    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "V:|-5-[username]-(-2)-[comment]-5-|", options: [], metrics: nil, views: ["username":usernameButton, "comment":commentLabel])) 

    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "V:|-15-[date]", options: [], metrics: nil, views: ["date":dateLabel])) 

    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "V:|-10-[profile(40)]", options: [], metrics: nil, views: ["profile":profileImagevView])) 

    //Horizontal 
    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "H:|-10-[profile(40)]-13-[comment]-20-|", options: [], metrics: nil, views: ["profile":profileImagevView, "comment":commentLabel])) 

    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "H:[profile]-13-[username]", options: [], metrics: nil, views: ["profile":profileImagevView, "username":usernameButton])) 

    self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
     "H:|[date]-10-|", options: [], metrics: nil, views: ["date":dateLabel])) 

コード

私の質問は

のですか?(iPhone5/6 Plusは正常に動作しています) - >シミュレートされたメトリックを設定していました。

答えて

1

awakeFromNibは、UIコードを設定する場所ではありません。この時点ではビューのどれも初期化されていません(ポインタはありません)。 UIコードをviewDidLoadに移動する必要があります。

+0

返信いただきありがとうございます。 :) –

関連する問題