【发布时间】:2014-12-02 09:45:04
【问题描述】:
我正在尝试使用其父视图中的按钮使我的子视图居中。所以我希望子视图的中心成为超级视图的中心。我正在尝试使用以下代码:
override func viewDidLoad() {
self.view.backgroundColor = UIColor.redColor()
var menuView = UIView()
var newPlayButton = UIButton()
//var newPlayImage = UIImage(named: "new_game_button_5cs")
var newPlayImageView = UIImageView(image: UIImage(named: "new_game_button_5cs"))
newPlayButton.frame = CGRectMake(0, 0, newPlayImageView.frame.width, newPlayImageView.frame.height)
newPlayButton.setImage(newPlayImage, forState: .Normal)
newPlayButton.backgroundColor = UIColor.whiteColor()
menuView.backgroundColor = UIColor.whiteColor()
menuView.addSubview(newPlayButton)
menuView.addConstraint(
NSLayoutConstraint(item: self.view,
attribute: .CenterX,
relatedBy: .Equal,
toItem: menuView,
attribute: .CenterX,
multiplier: 1, constant: 0)
)
}
不幸的是,当我尝试运行该程序时,它会中断。 (线程 1:信号 SIGABRT)
【问题讨论】:
-
添加异常断点查看崩溃的地方:developer.apple.com/library/ios/recipes/…
标签: ios swift autolayout constraints