在 Swift 中将多个 UIButton 添加到多个 UIView

Posted

技术标签:

【中文标题】在 Swift 中将多个 UIButton 添加到多个 UIView【英文标题】:Adding multiple UIButton to multiple UIView in Swift 【发布时间】:2017-10-21 12:01:12 【问题描述】:

我在下面编写了代码,以编程方式创建多个UIButton,这些UIButton 被放置在不同的UIView 上。所有按钮都是相似的,但按钮标题不同。代码确实完成了它需要做的事情,但是正如您所看到的,代码相当冗长,太冗长了。


问题

如何构造下面的代码并使其简洁明了?


代码

let myButton0 = UIButton(type: UIButtonType.Custom)
myButton0.setTitle("Text 0", forState:.Normal)
myButton0.titleLabel!.font = UIFont.systemFontOfSize(12)
myButton0.setTitleColor(UIColor.whiteColor(), forState: .Normal)
myButton0.backgroundColor =  UIColor.darkGrayColor()
myButton0.frame = CGRectMake(0, 0, 200, 100)
myButton0.alpha = 1
myButton0.showsTouchWhenHighlighted = false
myButton0.adjustsImageWhenHighlighted = false
myButton0.addTarget(self, action: #selector(ViewController.goDoThis0), forControlEvents:.TouchUpInside)
myView0.addSubview(myButton0)

let myButton1 = UIButton(type: UIButtonType.Custom)
myButton1.setTitle("Text 1", forState:.Normal)
myButton1.titleLabel!.font = UIFont.systemFontOfSize(12)
myButton1.setTitleColor(UIColor.whiteColor(), forState: .Normal)
myButton1.backgroundColor =  UIColor.darkGrayColor()
myButton1.frame = CGRectMake(0, 0, 200, 100)
myButton1.alpha = 1
myButton1.showsTouchWhenHighlighted = false
myButton1.adjustsImageWhenHighlighted = false
myButton1.addTarget(self, action: #selector(ViewController.goDoThis1), forControlEvents:.TouchUpInside)
myView1.addSubview(myButton1)

let myButton2 = UIButton(type: UIButtonType.Custom)
myButton2.setTitle("Text 2", forState:.Normal)
myButton2.titleLabel!.font = UIFont.systemFontOfSize(12)
myButton2.setTitleColor(UIColor.whiteColor(), forState: .Normal)
myButton2.backgroundColor =  UIColor.darkGrayColor()
myButton2.frame = CGRectMake(0, 0, 200, 100)
myButton2.alpha = 1
myButton2.showsTouchWhenHighlighted = false
myButton2.adjustsImageWhenHighlighted = false
myButton2.addTarget(self, action: #selector(ViewController.goDoThis2), forControlEvents:.TouchUpInside)
myView2.addSubview(myButton2)

let myButton3 = UIButton(type: UIButtonType.Custom)
myButton3.setTitle("Text 3", forState:.Normal)
myButton3.titleLabel!.font = UIFont.systemFontOfSize(12)
myButton3.setTitleColor(UIColor.whiteColor(), forState: .Normal)
myButton3.backgroundColor =  UIColor.darkGrayColor()
myButton3.frame = CGRectMake(0, 0, 200, 100)
myButton3.alpha = 1
myButton3.showsTouchWhenHighlighted = false
myButton3.adjustsImageWhenHighlighted = false
myButton3.addTarget(self, action: #selector(ViewController.goDoThis3), forControlEvents:.TouchUpInside)
myView3.addSubview(myButton3)

let myButton4 = UIButton(type: UIButtonType.Custom)
myButton4.setTitle("Text 4", forState:.Normal)
myButton4.titleLabel!.font = UIFont.systemFontOfSize(12)
myButton4.setTitleColor(UIColor.whiteColor(), forState: .Normal)
myButton4.backgroundColor =  UIColor.darkGrayColor()
myButton4.frame = CGRectMake(0, 0, 200, 100)
myButton4.alpha = 1
myButton4.showsTouchWhenHighlighted = false
myButton4.adjustsImageWhenHighlighted = false
myButton4.addTarget(self, action: #selector(ViewController.goDoThis4), forControlEvents:.TouchUpInside)
myView4.addSubview(myButton4)

【问题讨论】:

这个问题可能最适合核心评论论坛 - codereview.stackexchange.com - 但在我的脑海中,为什么不扩展 UIButton,创建一个 convenience 初始化程序,将每个按钮的代码包装到一个电话?您甚至可以将 addTarget 参数传递给初始化程序。 (我一直这样做。) 子类化 uibutton 【参考方案1】:

(Swift 4.0)首先,写一个创建按钮的常用方法:

func createButton(title:String,toView:UIView,action:Selector) 
    let myButton = UIButton(type: UIButtonType.custom)
    myButton.setTitle(title, for:.normal)
    myButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
    myButton.setTitleColor(UIColor.white, for: .normal)
    myButton.backgroundColor =  UIColor.darkGray
    myButton.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
    myButton.alpha = 1
    myButton.showsTouchWhenHighlighted = false
    myButton.adjustsImageWhenHighlighted = false
    myButton.addTarget(self, action: action, for: .touchUpInside)
    toView.addSubview(myButton)

然后创建这样的按钮:

let buttonInfos = [
                      ["Text 0",myView0,#selector(ViewController.goDoThis0)],
                      ["Text 1",myView1,#selector(ViewController.goDoThis1)],
                      ["Text 2",myView2,#selector(ViewController.goDoThis2)],
                      ["Text 3",myView3,#selector(ViewController.goDoThis3)],
                      ["Text 4",myView4,#selector(ViewController.goDoThis4)],
                  ]

for buttonInfo in buttonInfos 
    self.createButton(title: buttonInfo[0] as! String, toView: buttonInfo[1] as! UIView, action: buttonInfo[2] as! Selector)

【讨论】:

完美。解释和代码向我展示了一种新技术。非常感谢!【参考方案2】:

虽然这个问题已经得到解答,但我想提供另一种改编自 Yun CHEN's answer 的方法。

同样,为您的按钮创建一个通用方法:

func createButton(title:String,toView:UIView,action:Selector) 
    let myButton = UIButton(type: UIButtonType.custom)
    myButton.setTitle(title, for:.normal)
    myButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
    myButton.setTitleColor(UIColor.white, for: .normal)
    myButton.backgroundColor =  UIColor.darkGray
    myButton.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
    myButton.alpha = 1
    myButton.showsTouchWhenHighlighted = false
    myButton.adjustsImageWhenHighlighted = false
    myButton.addTarget(self, action: action, for: .touchUpInside)
    toView.addSubview(myButton)

然后在一个元组数组中列出按钮信息:

let buttonInfos = [
                      ("Text 0",myView0,#selector(ViewController.goDoThis0)),
                      ("Text 1",myView1,#selector(ViewController.goDoThis1)),
                      ("Text 2",myView2,#selector(ViewController.goDoThis2)),
                      ("Text 3",myView3,#selector(ViewController.goDoThis3)),
                      ("Text 4",myView4,#selector(ViewController.goDoThis4)),
                  ]

最后像这样创建按钮:

for buttonInfo in buttonInfos 
    self.createButton(title: buttonInfo.0, toView: buttonInfo.1, action: buttonInfo.2)

正如您在案例中看到的那样,通过使用元组,您无需使用 as! Stringas! UIView 等进行类型转换,从而简化了代码并使代码更短、更安全。

【讨论】:

感谢您的选择!

以上是关于在 Swift 中将多个 UIButton 添加到多个 UIView的主要内容,如果未能解决你的问题,请参考以下文章

在Swift中将UIButton从屏幕右侧定位20px

Swift:在 UIbutton 中将 fontawesome 图标与字符串混合

swift 在代码中将生成的imageView变成UIButton

如何在 Swift 中将 UIButton 居中?

如何在swift ios中将多个图像添加到自定义表格视图单元格?

Swift 访问 UIButton 标签