zoukankan      html  css  js  c++  java
  • swift UIButton

    //

    //  ButtonViewController.swift

    //  UIControlDemo

    //

    //  Created by  on 14/11/28.

    //  Copyright (c) 2014 马大哈. All rights reserved.

    //

     

    import UIKit

     

    class ButtonViewController: BaseViewController {

        override func viewDidLoad() {

            super.viewDidLoad()

            self.title = "Button"

            /*

            ------------------------------------------------------

            as 类型转换

            ------------------------------------------------------

            '(CGFloat) -> UIFont' is not convertible to 'UIFont'

            设置font  必须按下面的,强制转换成CGFloat(20),否则会报上面的错误

            ------------------------------------------------------

            */

            for var indexInt = 0; indexInt < 5; ++indexInt{

                // 在这里必须要强制转化(否则报错) 表示疑问,后续研究

                var yLoca = CGFloat(350) + CGFloat(40*indexInt)

                var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton

                button.tag = 100 + indexInt

                button.frame = CGRectMake(100, yLoca, 200, 30)

                button.setTitleColor(UIColor.blackColor(), forState:.Normal)//可以写成 button.setTitleColor(.blackColor(), forState:.Normal)

                button.setTitleColor(UIColor.whiteColor(), forState:.Selected)

                button.setTitle("点击按钮", forState: UIControlState.Normal)

                button.titleLabel!.font = UIFont.boldSystemFontOfSize(CGFloat(20))

                button.setImage(UIImage(named:""), forState: UIControlState.Normal)

                button.contentMode = UIViewContentMode.ScaleAspectFit

                button.addTarget(self, action: "buttonMethod:", forControlEvents: UIControlEvents.TouchUpInside)// UIControlEvents 可以去掉

                self.view.addSubview(button)

            }

        }

     

        func buttonMethod(touchButton:UIButton){

            println("调用按钮方法:(touchButton.tag)")

            touchButton.backgroundColor = .redColor()

            touchButton.selected = !touchButton.selected        

        }

        

        override func didReceiveMemoryWarning() {

            super.didReceiveMemoryWarning()

            // Dispose of any resources that can be recreated.

        }

        

    }

     

     

    效果图

     

  • 相关阅读:
    招隐-古琴曲-山中鸣琴,万籁声沉沉,何泠泠!
    因循苟且逸豫而无为,可以侥幸一时,而不可以旷日持久。——王安石
    模糊理论在图像处理中的应用
    铁关-中国首都警官合唱团-歌词
    听着总感觉莫名熟悉的音乐汇总
    石鼓歌-韩愈
    唐长安城
    唐长安的信仰——读书笔记
    Eclipse安装java web插件
    Java调用MySql数据库函数
  • 原文地址:https://www.cnblogs.com/madaha/p/4143723.html
Copyright © 2011-2022 走看看