我正在處理Swift中的按鈕。好吧,看起來Swift並沒有正確設置我的UIButton的位置,因爲它出現在屏幕上,當我想要它在中心時。這是我的代碼,我做錯了什麼?UIButton的位置顯示不正確
import SpriteKit
import UIKit
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let button = UIButton(type: UIButtonType.System)
let buttonImage = UIImage(named: "Button")
button.frame = CGRectMake(self.frame.size.width/2, self.frame.size.height/2, 100, 86) // x, y, width, height
button.setBackgroundImage(buttonImage, forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(button)
}
func buttonAction (sender: UIButton!) {
print("Button tapped")
}
請幫助:/
相反self.frame.size的'...''試試... self.view.frame.size' – u54r
@ u54r修復該按鈕的問題顯示關閉屏幕,但定位仍然很奇怪,謝謝你回答 – CodeCake