您好,我有一个关于布局的问题。
我使用 Storyboard来设置布局。
而且我已经通过 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/