swift 创建类方法(扩展)和便利构造函数

本文介绍了一个UIButton的Swift扩展,通过类方法和构造方法实现按钮的快速定制化创建,包括设置图片和背景图片,并展示了如何在MainViewController中使用这些扩展。
//
//  UIButtonExtension.swift
//  ZYFWB
//
//  Created by fe on 2017/3/8.
//  Copyright © 2017年 fe. All rights reserved.
//

import UIKit

//UIButton的类方法扩展
extension UIButton {
    
    //这里的类方法相当于OC中的加号方法
    class func creatButtonWithImageName(imageName : String , bgImageName : String) -> UIButton {
        
        //创建按钮
        let button = UIButton()
        
        //设置按钮的背景图片
        button.setBackgroundImage(UIImage(named: bgImageName), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named: bgImageName + "_highlighted"), for: UIControlState.highlighted)
        
        //设置按钮的图片
        button.setImage(UIImage(named: imageName), for: UIControlState.normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), for: UIControlState.highlighted)
        
        //按照背景图片初始化按钮尺寸
        button.sizeToFit()
        
        
        //返回按钮
        return button
        
    
    }
    
    
    
    /*
     convenience : 便利,使用convenience修饰的构造函数叫做便利构造函数
     便利构造函数通常在对系统的类进行构造函数的扩充时使用
     便利构造函数的特点
     1.便利构造函数通常是写在extension中
     2.便利构造函数init前面需要加载convenience
     3.在便利构造函数中需要明确调用self.init
     */
    convenience init(imageName : String , bgImageName : String) {
        self.init()
        //设置按钮的图片
        setImage(UIImage(named: imageName), for: UIControlState.normal)
        setImage(UIImage(named: imageName + "_highlighted"), for: UIControlState.highlighted)
        //设置按钮的背景图片
        setBackgroundImage(UIImage(named: bgImageName), for: UIControlState.normal)
        setBackgroundImage(UIImage(named: bgImageName + "_highlighted"), for: UIControlState.highlighted)
        
        //按照背景图片初始化按钮尺寸
        sizeToFit()
        
    }
    
}

//
//  MainViewController.swift
//  ZYFWB
//
//  Created by fe on 2017/3/7.
//  Copyright © 2017年 fe. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    //懒加载tabBar选中图片的数组
    lazy var selectImages = ["tabbar_home","tabbar_message_center","","tabbar_discover","tabbar_profile"]

    //懒加载中间的发布按钮(使用类方法)
    //lazy var composeBtn : UIButton = UIButton(imageName : "tabbar_compose_icon_add" , bgImageName : "tabbar_compose_button")
    
    //懒加载中间的发布按钮(使用构造方法)
    lazy var composeBtn : UIButton = UIButton.creatButtonWithImageName(imageName : "tabbar_compose_icon_add"  , bgImageName : "tabbar_compose_button")
    
    //系统调用函数
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置发布按钮
        setupComposeBtn()

        
    }
    
    //系统调用函数
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //调整tabBar的显示细节问题需要在该方法中,如果在viewDidLoad中调整,则在该方法中会被调整回来
        setupTabBarItems()
        
    }

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

}

// MARK:-设置UI界面
extension MainViewController {
    func setupComposeBtn() {
        
        //设置发布按钮位置
        composeBtn.center = CGPoint(x: tabBar.center.x, y: tabBar.frame.size.height * 0.5)
        
        //添加发布按钮到tabBar
        tabBar.addSubview(composeBtn)
    }
    
    //调整tabbaritems
    func setupTabBarItems() {
        //遍历tabBarItem
        for i in 0..<tabBar.items!.count{
            
            //拿到item
            let item = tabBar.items![i]
            
            //如果i为2则控制item不能点击
            if i == 2 {
                item.isEnabled = false
                continue
            }
            
            //设置item的选中图片
            item.selectedImage = UIImage(named: selectImages[i] + "_selected")
            
        }

    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值