Swift iPad - 按钮不起作用
Posted
技术标签:
【中文标题】Swift iPad - 按钮不起作用【英文标题】:Swift iPad - buttons not working 【发布时间】:2016-08-10 10:41:43 【问题描述】:我有 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)")
【问题讨论】:
用于 1...6 中的索引。我认为您需要从 0 循环到 5,因为它是数组的索引。 你能分享以下内容吗:questionButtonPressed func? @RoyK - 在 questionButtonPressed 和 BigButtonTouched 的问题中添加了功能代码。 【参考方案1】:在 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
【讨论】:
我尝试在 for 循环的每次迭代中声明按钮,但仍然没有效果。除了开始的两个按钮之外的圆形按钮不可点击以上是关于Swift iPad - 按钮不起作用的主要内容,如果未能解决你的问题,请参考以下文章
App Icon Switcher 在 iPad 上不起作用,只有默认图标 - Swift?