加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift开发笔记之UIButton基本用法

发布时间:2020-12-14 07:10:52 所属栏目:百科 来源:网络整理
导读: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 i

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编程无休止

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读