Coder Social home page Coder Social logo

vberihuete / dcfilterview Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 2.0 33 KB

FilterView component developed by Dev Core Team. In here you will find a tab-like view to filter content

License: MIT License

Ruby 11.91% Swift 88.09%
ios stackview autolayout swift filtering tabs cocoapods ui filter android-like

dcfilterview's Introduction

DCFilterView

CI Status Version License Platform

A Stack View friendly filter view with a tab look and feel.

filter view demo

This component is intendent to cover filter like solutions, not exactly View Controller (VC) changing type but content changing type in the same VC.

Example

To run the example project, clone the repo, and run pod install from the Example directory first. UI test are included so in order to see it working without throwing a line of code just run the UI test.

Installation

DCFilterView is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'DCFilterView'

How to

After you have your cocoapods set up done and already added the pod for DCFilterView, you just need to import in the View Controller you will like to use the Filter View.

import DCFilterView

Then add a Stack View to your View Controller, this being the one that will hold your content and the DCFilterView.

@IBOutlet weak var containerSV: UIStackView!

You can declare a variable of type DCFilterView that will an instance of the Filter View (or do as you like!)

var filterView: DCFilterView!

Then to give some structure to your DCFilterView you have the init

filterView = DCFilterView(items: ["Popular", "Related", "Oldest"], color: .black, selectedColor: .red, selectedIndex: 1)

Constructor properties go as follows:

  • items: The String array of filters
  • color: The color for the unselected filters
  • selectedColor: The color for the selectedFilter
  • selectedIndex: The initial selected index (based on the items given in the items property)

Go and add your recently created DCFilterView instance to your container stack view.

self.containerSV.insertArrangedSubview(filterView, at: 0)

And give to it some restrictions in height. e.g 40

self.containerSV.addConstraint(NSLayoutConstraint(item: self.filterView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 40))

That's it to make your Filter View visible.

Note: In my case here I'm setting the filter view in the first position making it visible as a top filter view, but you can place it as you like.

filter view demo

Giving it interaction

In order to react to filter change actions you need to implement the procotol DCFilterViewDelegate e.g.

extension ViewController: DCFilterViewDelegate{
    
    func dcFilterView(selected index: Int) {
        print("filter selected at index \(index)")
    }
    
}

You can as well now call a direct selection to an item using the utility method

filterView.markSelected(at: 2)

Also consulting which index or item is selected, using read-only computed properties

filterView.selected
filterView.selectedFilterViewItem

And as with all the delegation you are used to deal you will have to let your DCFilterView know that you want to be its delegate

filterView.delegate = self

PD: Pull request are welcome, so feel free to download this and play with it.

Author

Vincent Berihuete, [email protected]

License

DCFilterView is available under the MIT license. See the LICENSE file for more info.

dcfilterview's People

Contributors

vberihuete avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.