awakeFromNib()不用于模拟度量

时间:2016-06-13 00:15:28

标签: ios iphone swift nslayoutconstraint 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。其他方式..

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

代码

我的问题是

为什么此代码无法在iPhone6上运行?(iPhone5 / 6 Plus工作正常) - >它来自set Simulated Metrics?

1 个答案:

答案 0 :(得分:1)

awakeFromNib不是设置UI代码的地方。这一点没有初始化任何视图(零指针)。您应该将UI代码移至viewDidLoad