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

标签 ios iphone swift nslayoutconstraint awakefromnib

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

我使用 Storyboard来设置布局。

I had set on Size of iPhone 4.7-inch

而且我已经通过 Storyboard设置了一个 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 代码的地方。此时没有任何 View 被初始化(无指针)。您应该将您的 UI 代码移动到 viewDidLoad

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

相关文章:

ios - 如何将 3 个 UIButton 对齐到 UITableCellView 的中心?

iphone - 在 Objective-C 中发布 ivars

iphone - 更改 UITableviewcell 中附件类型图标的方法

ios - 如何在 xcode 8 中的对象中引用数组中的对象?

ios - 如何防止父 UIView 接收来自 UIButton 的触摸?

ios - 禁用和隐藏 UIButton/UIBarButtonItem 的理想方法是什么?

ios - "Reset keyboard dictionary"iOS 模拟器设置中缺少选项

iphone - 触摸由两层处理

ios - 使用 AVCaptureSession 开始录制视频

xcode - 类型 'CFStringRef' 不符合 Xcode 6.1 中的协议(protocol) 'Hashable'