Xcode 7.3,iOS 9.3
我正在试验游乐场,以扩大我使用它们进行快速原型制作的能力。我创建了一个简单的视图控制器,它在一个视图中添加/居中按钮。我在项目和游乐场中复制了视图控制器代码。当我在模拟器中执行项目时,按钮居中。当我执行游乐场时,按钮不居中。有人请帮助我理解为什么会这样吗?
我收录了3个截图:
Per @ Paulw11的评论,这里是更新和工作的视图控制器代码:
class ViewController : UIViewController {
let button = UIButton(type: .System)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .whiteColor()
view.layer.borderColor = UIColor.redColor().CGColor
view.layer.borderWidth = 2
button.bounds = CGRect(x: 0, y: 0, width: 50, height: 20)
button.backgroundColor = .blueColor()
button.setTitle("Rotate", forState: UIControlState.Normal)
button.addTarget(self, action: #selector(rotate), forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(button)
print("View: size is \(view.bounds.width) x \(view.bounds.height), centered @\(view.center)")
print("Button: size is \(button.bounds.width) x \(button.bounds.height), centered @\(button.center)")
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
button.center = view.convertPoint(view.center, fromView: view.superview)
}
func rotate() {
print("Rotate")
}
}