嗨,我有一个关于布局的问题。

我使用情节提要设置布局。

ios - awakeFromNib()不适用于模拟指标-LMLPHP

我已经通过情节提要设置了一个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工作正常)
->它是从模拟指标集发生的吗?

最佳答案

awakeFromNib不是设置UI代码的地方。此时,尚未初始化任何视图(nil指针)。您应该将UI代码移动到viewDidLoad

关于ios - awakeFromNib()不适用于模拟指标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37780360/

10-12 02:05