我有iPad屏幕:
1)更改位置时按钮不可点击。 2)圆形按钮图像被拉伸。
我需要创建下面屏幕中给出的按钮组。
我正在使用以下代码来创建按钮集。
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)")
}
答案 0 :(得分:0)
在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
}
}
}
}