0

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

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

我设置了 iPhone 4.7 英寸的尺寸

我已经通过情节提要设置了一个 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 中?

4

1 回答 1

1

awakeFromNib不是设置 UI 代码的地方。此时没有任何视图被初始化(零指针)。您应该将您的 UI 代码移动到viewDidLoad.

于 2016-06-13T02:31:36.420 回答