Giter VIP home page Giter VIP logo

gonevisible's Introduction

GoneVisible

Platform Language License Twitter

GoneVisible is a UIView extension that uses AutoLayout to add "gone" state like Android.

You can easily change the size constraint constant of UIView to 0 without adding IBOutlet property of size constraint. GoneVisible supports iOS and is written in Swift.

Additional notes: This library will be useful for projects targeting iOS 8. If your project is targeting iOS 9 or higher, I recommend you consider implementing with UIStackView before using this library.  
 

Requirements

  • Swift 4.0
  • iOS 8.0+
  • Xcode 9

Installation

Manual

Simply drag UIView+GoneVisible.swift into your project.

Cocoapods

  • Add into your Podfile.
pod "GoneVisible"

Then $ pod install

  • Add import GoneVisible to the top of your files where you wish to use it.

Usage

・Gone.

view.gone()

・Visible.

view.visible()

・When setting to Gone, set the space constraint constant together to 0.

view.gone(spaces: [.trailing])

・With "gone" you can specify whether it is vertical or horizontal. It is useful when animating.

view.gone(axis: .vertical)

Example Code

Here is the code for this example project. You do not need to import constraints by IBoutlet to control size.

import UIKit
import GoneVisible

class ViewController: UIViewController {

    @IBOutlet weak private var blackView: UIView!
    @IBOutlet weak private var redView: UIView!
    @IBOutlet weak private var blueView: UIView!
    @IBOutlet weak private var yellowButton: UIButton!
    @IBOutlet weak private var errorMessageLabel: UILabel!
    @IBOutlet weak private var toggleSwitch: UISwitch!

    override func viewDidLoad() {
    super.viewDidLoad()

        // When you want to display in "gone" state from the beginning.
//        toggleSwitch.isOn = false
//        goneViews()
    }

    @IBAction func toggleSwitchValueChanged(_ sender: UISwitch) {
        if toggleSwitch.isOn {
            visibleViews()
            UIView.animate(withDuration: 0.3) { view.layoutIfNeeded() }
        } else {
            goneViews()
            UIView.animate(withDuration: 0.3) { view.layoutIfNeeded() }
        }
    }

    private func goneViews() {
        blackView.gone()
        redView.gone(axis: .vertical, spaces: [.bottom])
        blueView.gone(axis: .horizontal, spaces: [.trailing])
        yellowButton.gone(axis: .horizontal, spaces: [.trailing])
        errorMessageLabel.gone(axis: .vertical)
    }

    private func visibleViews() {
        blackView.visible()
        redView.visible()
        blueView.visible()
        yellowButton.visible()
        errorMessageLabel.visible()
    }

}
See also:

Author

Teruto Yamasaki, [email protected]

License

The MIT License (MIT)
See the LICENSE file for more info.

gonevisible's People

Watchers

 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.