gpt4 book ai didi

ios - Swift iPad - 按钮不起作用

转载 作者:行者123 更新时间:2023-11-30 13:09:43 24 4
gpt4 key购买 nike

我有 iPad 屏幕:

1) 按钮在改变位置时不可点击。2) 圆形按钮图像被拉伸(stretch)。

我需要创建下面屏幕中给出的按钮集。

My screen

我正在使用以下代码来创建按钮集。

class MyCommonViewController: UIViewController {

var button: UIButton?
var Circularbutton: UIButton?

override func viewDidLoad() {
super.viewDidLoad()

var xMargin:CGFloat = 20.0
var yTopMargin:CGFloat = 40.0
var CircularBtnxMargin:CGFloat = 180.0
var CircularBtnyTopMargin:CGFloat = 200.0

var i = 1

let TextArray = ["Button 1", "Button 2", "Button 3", "Button 4", "Button 5", "Button 6"]

for index in 1...6
{
button = UIButton()
button?.tag=index
var buttonFrame = self.view.frame
buttonFrame.origin.x += xMargin
buttonFrame.origin.y += yTopMargin
buttonFrame.size.width = 200
buttonFrame.size.height = 200

button?.frame = buttonFrame
button?.layer.cornerRadius = 15.0
button?.layer.zPosition = 10

button?.backgroundColor = UIColor.lightGrayColor()

button?.setTitle(TextArray[index-1], forState: UIControlState.Normal)
button?.addTarget(self, action: #selector(MyCommonViewController.BigButtonTouched), forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(button!)


Circularbutton = UIButton()
var CbuttonFrame = self.view.frame

CbuttonFrame.origin = CGPoint(x:CircularBtnxMargin, y:CircularBtnyTopMargin)
CbuttonFrame.size.width = 50
CbuttonFrame.size.height = 50

Circularbutton?.frame = CbuttonFrame


Circularbutton?.tag=index
Circularbutton?.layer.zPosition = 100


let image = UIImage(named: "que2.png") as UIImage?
Circularbutton?.setImage(image, forState: UIControlState.Normal)
Circularbutton?.addTarget(self, action: #selector(MyCommonViewController.questionButtonPressed), forControlEvents: UIControlEvents.TouchUpInside)


self.view.addSubview(Circularbutton!)

xMargin+=250.0
CircularBtnxMargin+=250.0
i+=1

if(i > 3 )
{
yTopMargin+=300.0
xMargin=20.0
CircularBtnyTopMargin+=300.0
CircularBtnxMargin=180.0
i=1
}
}

}

}

1) 但是除了前两个按钮之外,带问号的按钮是不可点击的。

2)我的第二个问题是圆形按钮图像显示粗糙不均匀的边框。

你能帮我一下吗?

编辑:

func questionButtonPressed(sender:UIButton!) {

let btn:UIButton = sender

print("Circular Button Pressed - \(btn.tag)")
}


func BigButtonTouched(sender:UIButton!) {

let bigBtn:UIButton = sender

print("Button Pressed - \(bigBtn.tag)")

}

最佳答案

在 for 循环的每次迭代中声明按钮,而不是在 for 循环之外声明按钮。查看代码(您需要删除所有强制展开):

class MyCommonViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

var xMargin:CGFloat = 20.0
var yTopMargin:CGFloat = 40.0
var CircularBtnxMargin:CGFloat = 180.0
var CircularBtnyTopMargin:CGFloat = 200.0

var i = 1

let TextArray = ["Button 1", "Button 2", "Button 3", "Button 4", "Button 5", "Button 6"]

for index in 1...6
{
let button = UIButton()
button?.tag=index
var buttonFrame = self.view.frame
buttonFrame.origin.x += xMargin
buttonFrame.origin.y += yTopMargin
buttonFrame.size.width = 200
buttonFrame.size.height = 200

button?.frame = buttonFrame
button?.layer.cornerRadius = 15.0
button?.layer.zPosition = 10

button?.backgroundColor = UIColor.lightGrayColor()

button?.setTitle(TextArray[index-1], forState: UIControlState.Normal)
button?.addTarget(self, action: #selector(MyCommonViewController.BigButtonTouched), forControlEvents: UIControlEvents.TouchUpInside)

self.view.addSubview(button!)


let Circularbutton = UIButton()
var CbuttonFrame = self.view.frame

CbuttonFrame.origin = CGPoint(x:CircularBtnxMargin, y:CircularBtnyTopMargin)
CbuttonFrame.size.width = 50
CbuttonFrame.size.height = 50

Circularbutton?.frame = CbuttonFrame


Circularbutton?.tag=index
Circularbutton?.layer.zPosition = 100


let image = UIImage(named: "que2.png") as UIImage?
Circularbutton?.setImage(image, forState: UIControlState.Normal)
Circularbutton?.addTarget(self, action: #selector(MyCommonViewController.questionButtonPressed), forControlEvents: UIControlEvents.TouchUpInside)


self.view.addSubview(Circularbutton!)

xMargin+=250.0
CircularBtnxMargin+=250.0
i+=1

if(i > 3 )
{
yTopMargin+=300.0
xMargin=20.0
CircularBtnyTopMargin+=300.0
CircularBtnxMargin=180.0
i=1
}
}

}

}

关于ios - Swift iPad - 按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38871131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com