关于ios:Swift:以编程方式创建具有某些特定字体和大小的UIButton

Swift : Programatically Create a UIButton with Some Specific Font And Size

我想以编程方式创建一个基本的UIButton。例如,在我的视图控制器中,将在同一行中动态创建五个UIButton,并为某些颜色、字体和大小设置其布局或属性。

我还需要为一个特定的按钮添加操作方法。


如果您想以编程方式进行,可以按照以下几行进行思考:

在中为…创建按钮…在viewdidLoad()中循环(或其他地方,取决于您的要求):

1
2
3
4
5
6
7
8
9
10
11
12
13
    let buttonWidth : CGFloat = self.view.frame.width / 10
    let buttonHeight : CGFloat = 50

    for count in 0..<10 {
        let newButton = UIButton(frame: CGRect(origin: CGPoint(x: CGFloat(count) * buttonWidth, y: 50), size: CGSize(width: buttonWidth, height: buttonHeight)))
        newButton.backgroundColor = UIColor.red
        newButton.setTitle("Button #\(count)", for: UIControlState.normal)
        newButton.setTitleColor(UIColor.white, for: UIControlState.normal)
        newButton.addTarget(self, action: #selector(self.buttonTapped(sender:)), for: UIControlEvents.touchUpInside)
        newButton.titleLabel?.font = UIFont(name:"Arial", size: 10)
        newButton.tag = count
        self.view.addSubview(newButton)
    }

然后,您可以按如下方式实现选择按钮tapped(sender:uibutton),检索按钮的标记,以便您可以定制操作(例如,通过switch语句):

1
2
3
4
5
func buttonTapped(sender: UIButton) -> Void {
    print("\(sender.tag) was tapped!")

    // Do something more interesting based on the tag here...
}

这允许您在没有很多不同选择器的情况下设置许多按钮操作。希望有帮助。


创建uibuttons并将其添加到主视图中,请参见下面的示例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class yourClassName: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // here method CGRectMake defines x, y, width, height
        var btnOne = UIButton(frame: CGRectMake(0, 0, 50, 50))
        var btnOne = UIButton(frame: CGRectMake(0, 0, 50, 50))

        self.view.addSubview(btnOne) // add button inside view
        self.view.addSubview(btnOne) // add button inside view

        // using selector you must specify a method for each button
        btnOne.addTarget(self, action: #selector(self.actionOne), forControlEvents: .TouchUpInside)

        btnTwo.addTarget(self, action: #selector(self.actionTwo), forControlEvents: .TouchUpInside)
}

动作方法:

1
2
3
4
5
6
7
func actionOne(sender : UIButton) {
    print("button one tapped")
}

func actionTwo(sender : UIButton) {
    print("button second tapped")
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 let bacViewOne=UIButton(frame: CGRectMake(20, 60 , 130, 130))
            bacViewOne.backgroundColor=UIColor.whiteColor()
            bacViewOne.layer.cornerRadius=5.0
            btn1 = UIButton(type: UIButtonType.Custom) as UIButton
            btn1.setImage(UIImage(named:"new.png"), forState: UIControlState.Normal)
            btn1.frame = CGRectMake((bacViewOne.frame.size.width-90)/2, 5 , 90, 90)
            //btn1.backgroundColor = UIColor.whiteColor()
            btn1.tag = 1
            btn1.addTarget(self, action: #selector(ViewController.dashboard(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            bacViewOne.addSubview(btn1)
            self.view.addSubview(bacViewOne)

            let l1 = UILabel(frame: CGRectMake((bacViewOne.frame.size.width-90)/2, 100, 90, 21))

            l1.textAlignment = NSTextAlignment.Center
            l1.textColor = UIColor(red: 41/255.0, green: 43/255.0, blue: 64/255.0, alpha: 1)
            l1.text ="Dashboard"
            //l1.font = UIFont (name:"BakerSignetBT-Roman", size: 15)
            bacViewOne.addSubview(l1)
            l1.font = UIFont.systemFontOfSize(11)



            var bacViewT=UIButton(type: UIButtonType.Custom) as UIButton
            bacViewT .frame = CGRectMake(20, 240, 130, 130)
            bacViewT.backgroundColor=UIColor.whiteColor()
            bacViewT.layer.cornerRadius=5.0
            btn3 = UIButton(type: UIButtonType.Custom) as UIButton
            btn3.setImage(UIImage(named:"Usernew.png"), forState: UIControlState.Normal)
            btn3.frame = CGRectMake((bacViewT.frame.size.width-90)/2, 5, 90, 90)
//            btn3.backgroundColor = UIColor(red: 41/255.0, green: 43/255.0, blue: 64/255.0, alpha: 1)
            btn3.tag = 2
            btn3.addTarget(self, action: #selector(ViewController.userExpenses(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            bacViewT.addSubview(btn3)

            self.view.addSubview(bacViewT)

            let l2 = UILabel(frame: CGRectMake((bacViewT.frame.size.width-100)/2, 100, 100, 21))

            l2.textAlignment = NSTextAlignment.Center
            l2.textColor = UIColor(red: 41/255.0, green: 43/255.0, blue: 64/255.0, alpha: 1)
            l2.text ="User Expense"
            //l2.font = UIFont (name:"BakerSignetBT-Roman", size: 15)
            bacViewT.addSubview(l2)
            l2.font = UIFont.systemFontOfSize(11)
            //7


            let bacViewTh=UIButton(frame: CGRectMake(170, 60, 130, 130))
            bacViewTh.backgroundColor=UIColor.whiteColor()
            bacViewTh.layer.cornerRadius=5.0
            btn7 = UIButton(type: UIButtonType.Custom) as UIButton
            btn7.setImage(UIImage(named:"events.png"), forState: UIControlState.Normal)
            btn7.frame = CGRectMake((bacViewTh.frame.size.width-90)/2, 5, 90, 90)
            btn7.tag = 3
            btn7.addTarget(self, action: #selector(ViewController.upcomingEvents(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            bacViewTh.addSubview(btn7)


            let l3 = UILabel(frame: CGRectMake((bacViewTh.frame.size.width-100)/2, 100, 100, 21))

            l3.textAlignment = NSTextAlignment.Center
            l3.textColor = UIColor(red: 41/255.0, green: 43/255.0, blue: 64/255.0, alpha: 1)
            l3.text ="Upcoming Events"
            //l3.font = UIFont (name:"BakerSignetBT-Roman", size: 15)
            bacViewTh.addSubview(l3)
            l3.font = UIFont.systemFontOfSize(11)
            self.view.addSubview(bacViewTh)
            //8

            let bacViewF=UIButton(frame: CGRectMake(170, 240, 130, 130))
            bacViewF.backgroundColor=UIColor.whiteColor()
            bacViewF.layer.cornerRadius=5.0
            btn9 = UIButton(type: UIButtonType.Custom) as UIButton
            btn9.setImage(UIImage(named:"Location.png"), forState: UIControlState.Normal)
            btn9.frame = CGRectMake((bacViewF.frame.size.width-90)/2, 5, 90, 90)
            btn9.tag = 4

            btn9.addTarget(self, action: #selector(ViewController.userLocations(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            bacViewF.addSubview(btn9)
            self.view.addSubview(bacViewF)

            let l4 = UILabel(frame: CGRectMake((bacViewF.frame.size.width-100)/2, 100, 100, 21))

            l4.textAlignment = NSTextAlignment.Center
            l4.textColor = UIColor(red: 41/255.0, green: 43/255.0, blue: 64/255.0, alpha: 1)
            l4.text ="User Locations"
            bacViewF.addSubview(l4)
            l4.font = UIFont.systemFontOfSize(11)