模块VC - 用于模块/部分选择的集合视图控制器

模块VC

考西徽章

模块VC是用于模块/部分选择的集合视图控制器。我将其用作主屏幕,供用户单击模块图标以转到应用程序中的特定区域。


安装

使用可可豆荚

pod 'ModulesVC'

设置

  • 创建新的视图和视图控制器
  • 导入模块VC
  • 使您的视图控制器符合模块VC
  • 配置模块(见下文)
  • 调用设置集合视图()

使用的第三方框架

配置模块

默认模块

let module1 = KModule.init(title: "Users", action: "segueUsers", icon: UIImage(named: "icons8-swirl")!)
let module2 = KModule.init(title: "Cable Count Calculator", action: "test", icon: UIImage(named: "icons8-swirl")!, remoteIconURL: "https://img.icons8.com/external-flaticons-flat-flat-icons/64/000000/external-test-nursing-flaticons-flat-flat-icons.png", badgeText: "New", isEnabled: false, watermark: UIImage(named: "icons8-disabled"))
modules = [module2,module2]
  • 标题在模块图标底部显示为 UILabel。
  • Action is used to call didSelectModule function when module icon is tapped. By default if the module’s isEnabled is set to false then an access denied alert will be displayed, otherwise if the word segue is in the action then it will perform segue on the action text. Ex. action “segueUsers” will perform segue on “segueUsers”.
  • Icon is the icon to display for the module
  • badgeText is optional and if not nil will display a badge in the upper right of the module icon with the badgeText
  • isEnabled sets the module to enabled for the user
  • watermark is optional and used to set an image as a watermark over the top of the module icon if module isEnabled=false

Custom Module

Module text and badge can be customized by passing “settings bundles”. The easiest way to do this is by using the buildModule() function.

        var settingsBundle: KModuleSettings = KModuleSettings()
        settingsBundle.badge.badgeColor = .gray
        settingsBundle.badge.textColor = .white
        settingsBundle.title.textColor = .brown
        settingsBundle.title.font = UIFont(name: "Marker Felt", size: 17)!
        settingsBundle.watermark.alpha = 0.85
        
        var module3 = buildModule(title: "Users", action: "segueUsers", icon: UIImage(named: "icons8-swirl")!, remoteIconURL: "https://img.icons8.com/external-flaticons-flat-flat-icons/64/000000/external-test-nursing-flaticons-flat-flat-icons.png", badgeText: "New", isEnabled: false, watermark: UIImage(named: "icons8-disabled"), badgeSettings: settingsBundle.badge, titleSettings: settingsBundle.title, watermarkSettings: settingsBundle.watermark)
        modules = [module3]

Customize

Customize Default Settings

    ///Sets Icon Width. Default is 100
    iconWidth = 100
    ///Sets cell background color. Default is clear
    cellBackgroundColor = .clear
    ///Sets collectionView background color. Default is clear
    collectionViewBackgroundColor = .clear
    ///Sets the title of the alert displayed when user clicks on disabled module
    disabledAlertTitle = "Access Denied"
    ///Sets the message of the alert displayed when user clicks on disabled module
    disabledAlertMessage = "You do not have access to this module!"

Remote Icon Cache

By default remote icons are downloaded and saved in cache for 90 days. Once the cache has expired the icon will be re-downloaded next time it is used. This can be changed by overriding setIconImageCachExpiration()

override func setIconCacheExpiration() {
        iconCache.diskStorage.config.expiration = .days(90)
        iconCache.memoryStorage.config.expiration = .days(90)
    }

Collection View Constraints

By default collection view constraints are set to view bounds. This can be changed by overriding setCollectionViewConstraints()

override func setCollectionViewConstraints() {
        collectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    }

Reload Collection View

If you need code to be called each time the collection view is reloaded you can override reloadCollectionView()

override func reloadCollectionView() {
            self.collectionView.reloadData()
    }

Did Select Module

You can override the function didSelectModule() to customize actions when user taps on module icon.

override func didSelectModule(_ module: KModule) {
        guard module.isEnabled else {
            ShowAlert.centerView(theme: .error, title: self.disabledAlertTitle, message: self.disabledAlertMessage, seconds: .infinity, invokeHaptics: true)
            return
        }
        if module.action.contains("segue") {
            performSegue(withIdentifier: module.action, sender: self)
        }
    }

GitHub

点击跳转