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.viewDidLoad()
        myButton = UIButton(frame: CGRectMake(0,0,200,50))
        myButton.backgroundColor = UIColor.yellowColor()
        myButton.layer.masksToBounds = true
        myButton.layer.cornerRadius = 10
        myButton.setTitle("无敌是多么寂寞",forState: UIControlState.Normal)
        myButton.addTarget(self,action: "clickMyButton:",forControlEvents: UIControlEvents.TouchUpInside)
        myButton.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal)
        myButton.center = CGPointMake(self.view.bounds.width/2,200)
        self.view.addSubview(myButton)

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

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

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

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

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

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


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

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

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

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

官方API:
UIButtonType Reference

UIButton Class Reference

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

原文链接:https://www.f2er.com/swift/324347.html

猜你在找的Swift相关文章