Shawn Baek
Shawn Baek

Reputation: 1967

awakeFromNib() is not working on simulate metric

Hi I have a question about layout.

I use the storyboard to set layout.

I had set on Size of iPhone 4.7-inch

And I had set an UI Components via the storyboard.

To support iPhone5 or iPhone 6 Plus.. I'm writing code like this.

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. Other ways..

 //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]))

code here

My question is

Why this code is not working on iPhone6? (iPhone5 / 6 Plus is working fine) -> It happened from set Simulated Metrics?

Upvotes: 0

Views: 153

Answers (1)

rvijay007
rvijay007

Reputation: 1357

awakeFromNib is not the place to set up UI code. None of the views have been initialized by this point (nil pointers). You should move your UI code to viewDidLoad.

Upvotes: 1

Related Questions