Giter Site home page Giter Site logo

abhishekfk / contentsheet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from flipkart-incubator/contentsheet

0.0 0.0 0.0 3.48 MB

A simple control that enables presenting any view controller or navigation controller or any other object that can provide a view like an ActionSheet

License: Apache License 2.0

Ruby 4.06% Swift 95.94%

contentsheet's Introduction

ContentSheet

Version License Platform

Demo

Demo

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

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

pod "ContentSheet"

Usage

ContentSheet can present any object that conforms to 'ContentSheetContentProtocol'. Example project and demo show how to present a UIView, UIViewController, UINavigationController or a UIView controller by some custom object/controller. UIView, UIViewController and UINavigationController extensions are provided out of the box. Take a look at them in 'ContentSheet.swift' file to get an idea.

To present a view controller or a navigation controller from any view controller

<presenter instance>.present(inContentSheet: <view controller instance>, animated: true)

To dismiss the content sheet from content view controller

self.dismissContentSheet(animated: true)

To present a view in content sheet

let contentSheet = ContentSheet(content: <UIView instance>)
<presenter instance>.present(contentSheet, animated: true, completion: completion)

To dismiss content sheet from content view

self.dismissContentSheet(animated: true)

To provide a scroll view to observe from a view controller

override func scrollViewToObserve(containedIn contentSheet: ContentSheet) -> UIScrollView? {
    return <scrollview instance>
}

ContentSheet shows a navigation bar on top of content by default. To control this behaviour use this property of ContentSheet instance

public var showDefaultHeader: Bool = true

If you present a UIViewController in ContentSheet, default header will take the navigation item of view controller and push that on the default navigation bar's stack. That means just as in case of UINavigationController, the title and left, right bar buttons of your view controller's navigationItem will show on top by default. If there is no left bar button, content sheet will show a close button there.

If you are presenting anything other than, UIViewController, use this property from ContentSheetContentProtocol

@objc optional var navigationItem: UINavigationItem { get }

If you need to access the default navigation bar or item, use this property of content sheet instance

public var contentNavigationBar: UINavigationBar?
public var contentNavigationItem: UINavigationItem?

You can customise the navigation item content's with these. Or just disable the default header using swift showDefaultHeader and make the whole content yourself.

In case a UIViewController instance is presented, this convenience method lets you access the navigation bar on top

public func cs_navigationBar()

This method first checks if the UIViewController instance is inside a navigationController. If so, it'll return navigationController's navigation bar else content sheet's navigation bar

Control collapsed and expanded heights using

open func collapsedHeight(containedIn contentSheet: ContentSheet) -> CGFloat {
    return <height>
}

open func expandedHeight(containedIn contentSheet: ContentSheet) -> CGFloat {
    return <height>
}

Control status bar appearance using

open func prefersStatusBarHidden(contentSheet: ContentSheet) -> Bool {
    return <should hide?>
}

open func preferredStatusBarStyle(contentSheet: ContentSheet) -> UIStatusBarStyle {
    return <style>
}

open func preferredStatusBarUpdateAnimation(contentSheet: ContentSheet) -> UIStatusBarAnimation {
    return <animation>
}

There are convenience methods on UIViewController and UIView to get content sheet using 'self.contentSheet()' There is also a function in ContentSheet i.e. 'contentSheet(content:)' that can be used to get content sheet for any content.

Also, take a look at 'presentCustomView(_:)' function in ViewController.swift and CustomContent.swift to see how to present UIView or it's subclasses.

Use these properties of content sheet to customize behaviour

public var showDefaultHeader: Bool = true
public var blurBackground: Bool = false
public var blurStyle: UIBlurEffectStyle = .dark
public var dismissOnTouchOutside: Bool = true
public var backgroundImage: UIImage? 
public var backgroundView: UIView? 

ContentSheetDelegate

Use delegate to get callbacks. Delegate conforms to 'ContentSheetDelegate'.

These callbacks are sent when sheet is shown or hidden.

@objc optional func contentSheetWillShow(_ sheet: ContentSheet)
@objc optional func contentSheetDidShow(_ sheet: ContentSheet)
@objc optional func contentSheetWillHide(_ sheet: ContentSheet)
@objc optional func contentSheetDidHide(_ sheet: ContentSheet)

These callbacks are given when content sheet view appears or disappears. Use them if you'd wanna update some thing in content on appearance transitions.

@objc optional func contentSheetWillAppear(_ sheet: ContentSheet)
@objc optional func contentSheetDidAppear(_ sheet: ContentSheet)
@objc optional func contentSheetWillDisappear(_ sheet: ContentSheet)
@objc optional func contentSheetDidDisappear(_ sheet: ContentSheet)

ContentSheetContentProtocol

Return a content view using this from content controller

var view: UIView! {get}

Return a navigation item this from content controller

@objc optional var navigationItem: UINavigationItem { get }

Prepare for content view lifecycle events using these (Checkout UIViewController extension in ContentSheet.swift)

@objc optional func contentSheetWillAddContent(_ sheet: ContentSheet)
@objc optional func contentSheetDidAddContent(_ sheet: ContentSheet)
@objc optional func contentSheetWillRemoveContent(_ sheet: ContentSheet)
@objc optional func contentSheetDidRemoveContent(_ sheet: ContentSheet)

Configure behaviour using these (Checkout SecondViewController.swift and UIViewController extension in ContentSheet.swift)

@objc optional func collapsedHeight(containedIn contentSheet: ContentSheet) -> CGFloat
@objc optional func expandedHeight(containedIn contentSheet: ContentSheet) -> CGFloat

@objc optional func scrollViewToObserve(containedIn contentSheet: ContentSheet) -> UIScrollView?

To know content and state use these readonly vars

public var content: ContentSheetContentProtocol
public var state

Example of presenting any content

let contentSheet = ContentSheet(content: <ContentSheetContentProtocol instance>)
<presenter instance>.present(contentSheet, animated: true, completion: completion)

Author

rajatgupta26, [email protected]

License

Copyright 2017 Flipkart Internet Pvt. Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

contentsheet's People

Contributors

rajatgupta26 avatar naveen-c avatar thekirankumar 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.