【问题标题】:Swift to create common class for UIButton stylesSwift 为 UIButton 样式创建通用类
【发布时间】:2019-01-31 12:41:25
【问题描述】:

我在多个类文件中使用多个按钮,具有相同的外观和感觉,但动作和标题不同。所以,我需要创建公共类,所有其他类都需要引用它来查看 UIButton 阴影外观。

另外,请告诉我哪一个是 UIReusability 的最佳扩展或通用类。

下面是我正在使用的代码

sbutton.layer.cornerRadius = 21
sbutton.clipsToBounds = true
sbutton.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
sbutton.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
sbutton.layer.shadowOpacity = 1.0
sbutton.layer.shadowRadius = 1.0
sbutton.layer.masksToBounds = false

【问题讨论】:

    标签: ios swift xcode cocoa-touch uibutton


    【解决方案1】:

    在 Swift 4.1 和 Xcode 9.4.1 中

    我正在为此使用共享类(每次使用一次编写)

    import UIKit
    class SharedClass: NSObject {
    
        static let sharedInstance = SharedClass()
    
        func styleForComponents(button: UIButton) {
            button.layer.cornerRadius = 21
            button.clipsToBounds = true
            button.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
            button.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
            button.layer.shadowOpacity = 1.0
            button.layer.shadowRadius = 1.0
            button.layer.masksToBounds = false
        }
    
        private override init() {
    
        }
    
    }
    

    在整个项目中这样调用这个函数

    SharedClass.sharedInstance.styleForComponents(button: sbutton)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      相关资源
      最近更新 更多