美文网首页
swift中UIButton的使用

swift中UIButton的使用

作者: 不安分心 | 来源:发表于2016-12-18 21:58 被阅读0次

    UIButton的相关操作

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
       
            self.setupButton()
        }
    
        func setupButton() {
            // 创建button
            /**
             public enum UIButtonType : Int {
             case custom // no button type  定制按钮,前面不带图标,默认文字颜色为白色,无触摸时
             @available(iOS 7.0, *)
             case system // standard system button  前面不带图标,默认文字颜色为蓝色,有触摸时的高亮效果
             case detailDisclosure  前面带“!”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
             case infoLight 为感叹号“!”圆形按钮
             case infoDark  为感叹号“!”圆形按钮
             case contactAdd    前面带“+”图标按钮,默认文字颜色为蓝色,有触摸时的高亮效果
             public static var roundedRect: UIButtonType { get } // Deprecated, use UIButtonTypeSystem instead
             }
             */
            let button: UIButton = UIButton(type: .contactAdd)
            button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
            button.backgroundColor = UIColor.red
            // 设置背景图片
            let image = UIImage(named: "back")
            button.setBackgroundImage(image, for: .normal)
            /**
             public struct UIControlState : OptionSet {
             
             public init(rawValue: UInt)
             public static var normal: UIControlState { get } 普通状态下的文字
             public static var highlighted: UIControlState { get } 触摸状态下的文字
             public static var disabled: UIControlState { get } 禁用状态下的文字
             public static var selected: UIControlState { get } // flag usable by app (see below)
             public static var focused: UIControlState { get } // Applicable only when the screen supports focus
             public static var application: UIControlState { get } // additional flags available for application use
             public static var reserved: UIControlState { get } // flags reserved for internal framework use
             }
    
             */
            // 设置文字
            button.setTitle("点击", for: .normal)
            // 设置点击事件 (带参数)
            button.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
            // 设置点击事件 (不带参数)
            button.addTarget(self, action: #selector(buttonotherClick), for: .touchUpInside)
            // 设置图片
            button.setImage(UIImage(named: "back"), for: .normal)
            // 使禁用模式下按钮也不会变暗
            button.adjustsImageWhenDisabled = false
            // 使触摸模式下按钮也不会变暗
            button.adjustsImageWhenHighlighted = false
            // 设置远角
            button.layer.masksToBounds = true
            button.layer.cornerRadius = 50
            // 设置按钮点击时高亮,默认点击时是高亮状态
            button.showsTouchWhenHighlighted = true
        }
        
        func buttonClick(_ btn: UIButton) {
            
        }
        func buttonotherClick() {
        
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:swift中UIButton的使用

          本文链接:https://www.haomeiwen.com/subject/riyzmttx.html