Swift开发笔记之UIButton基本用法

前端之家收集整理的这篇文章主要介绍了Swift开发笔记之UIButton基本用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

swift与OC有相同的UIButtonType

enum UIButtonType : Int {
    case Custom
    case System
    case DetailDisclosure
    case InfoLight
    case InfoDark
    case ContactAdd
    static var RoundedRect: UIButtonType { get }
}

效果预览:

代码实现部分ViewController.swift

import UIKit

class ViewController: UIViewController {

    private var myButton : UIButton!
    private var myInfoDarkButton: UIButton!
    private var myInfoLightButton: UIButton!
    private var myAddButton: UIButton!
    private var myDetailButton: UIButton!
    private var mySystemButton: UIButton!

    override func viewDidLoad() {
        super@H_403_33@.viewDidLoad()
        myButton = UIButton(frame: CGRectMake(0,0,200,50))
        myButton@H_403_33@.backgroundColor = UIColor@H_403_33@.yellowColor()
        myButton@H_403_33@.layer@H_403_33@.masksToBounds = true
        myButton@H_403_33@.layer@H_403_33@.cornerRadius = 10
        myButton@H_403_33@.setTitle("无敌是多么寂寞",forState: UIControlState@H_403_33@.Normal)
        myButton@H_403_33@.addTarget(self,action: "clickMyButton:",forControlEvents: UIControlEvents@H_403_33@.TouchUpInside)
        myButton@H_403_33@.setTitleColor(UIColor@H_403_33@.blackColor(),forState: UIControlState@H_403_33@.Normal)
        myButton@H_403_33@.center = CGPointMake(self@H_403_33@.view@H_403_33@.bounds@H_403_33@.width/2,200)
        self@H_403_33@.view@H_403_33@.addSubview(myButton)

        //设置button的类型
        myInfoDarkButton = UIButton(type: UIButtonType@H_403_33@.InfoDark)
        myInfoLightButton = UIButton(type: UIButtonType@H_403_33@.InfoLight)
        myAddButton = UIButton(type: UIButtonType@H_403_33@.ContactAdd)
        myDetailButton = UIButton(type: UIButtonType@H_403_33@.DetailDisclosure)
        mySystemButton = UIButton(type: UIButtonType@H_403_33@.System)

        // mySystemButton
        mySystemButton@H_403_33@.frame = CGRectMake(0,50)
        // mySystemButton设定位置
        mySystemButton@H_403_33@.layer@H_403_33@.position = CGPoint(x: self@H_403_33@.view@H_403_33@.frame@H_403_33@.width/2,y:250)
        // mySystemButton标题
        mySystemButton@H_403_33@.setTitle("mySystemButton",forState: UIControlState@H_403_33@.Normal)

        //myInfoDarkButton
        myInfoDarkButton@H_403_33@.frame = CGRectMake(0,50);
        myInfoDarkButton@H_403_33@.layer@H_403_33@.position = CGPoint(x: self@H_403_33@.view@H_403_33@.bounds@H_403_33@.width/2,y: 300)
        myInfoDarkButton@H_403_33@.setTitle("myInfoDarkButton",forState: UIControlState@H_403_33@.Normal)

        // myInfoLightButton
        myInfoLightButton@H_403_33@.frame = CGRectMake(0,50)
        myInfoLightButton@H_403_33@.layer@H_403_33@.position = CGPoint(x: self@H_403_33@.view@H_403_33@.frame@H_403_33@.width/2,y:350)
        myInfoLightButton@H_403_33@.setTitle("myInfoLightButton",forState: UIControlState@H_403_33@.Normal)

        // myAddButton
        myAddButton@H_403_33@.frame = CGRectMake(0,50)
        myAddButton@H_403_33@.layer@H_403_33@.position = CGPoint(x: self@H_403_33@.view@H_403_33@.frame@H_403_33@.width/2,y:400)
        myAddButton@H_403_33@.setTitle("mySystemButton",forState: UIControlState@H_403_33@.Normal)

        // myDetailButton
        myDetailButton@H_403_33@.frame = CGRectMake(0,50)
        myDetailButton@H_403_33@.layer@H_403_33@.position = CGPoint(x: self@H_403_33@.view@H_403_33@.frame@H_403_33@.width/2,y:450)
        myDetailButton@H_403_33@.setTitle("myDetailButton",forState: UIControlState@H_403_33@.Normal)


        self@H_403_33@.view@H_403_33@.addSubview(myInfoDarkButton)
        self@H_403_33@.view@H_403_33@.addSubview(myInfoLightButton)
        self@H_403_33@.view@H_403_33@.addSubview(myAddButton)
        self@H_403_33@.view@H_403_33@.addSubview(myDetailButton)
        self@H_403_33@.view@H_403_33@.addSubview(mySystemButton)

        // Do any additional setup after loading the view,typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super@H_403_33@.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    internal func clickMyButton (sender:UIButton)
    {
        myButton@H_403_33@.backgroundColor = UIColor@H_403_33@.purpleColor()
        myButton@H_403_33@.setTitleColor(UIColor@H_403_33@.blackColor(),forState: UIControlState@H_403_33@.Normal)
        myButton@H_403_33@.setTitle("无敌是多么空虚",forState: UIControlState@H_403_33@.Normal)
    }
}

官方API:
UIButtonType Reference

UIButton Class Reference

欢迎来我的个人博客,希望多多赐教:Coding24h编程无休止

猜你在找的Swift相关文章