本文主要是介绍swift 创建类方法(扩展)和便利构造函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
https://blog.csdn.net/zhengyanfeng1989/article/details/60867795
-
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()
-
}
-
}
- <code class="language-objc">//
- // 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")
- }
- }
- }
- </code>
https://blog.csdn.net/ZhengYanFeng1989/article/details/60867795
这篇关于swift 创建类方法(扩展)和便利构造函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!