Giter VIP home page Giter VIP logo

msautoview's Introduction

MSAutoView

This is a way to create custom views with their own xibs and use them across multiple view controllers, cells etc... Properties can be easily set and customized independently for each view controller

Table of Contents

  1. The Problem
  2. How MSAutoView solves the problem
  3. Installation
  4. Prerequisites
  5. Usage
  6. Customizing The View
  7. Deployment
  8. Authors
  9. License

The Problem

Throughout my projects I had a problem where I have to use the same view in multiple places, and whenever I implement the layouts in the storyboards, things get messy when the storyboard grows and I have to keep track where I implemented each view. Things get more messy when I want to modify a subview inside these views (layout, color etc..), I had to go through all the storyboard to change them.

How MSAutoView solves the problem

MSAutoView allows me to create a single layout xib, and reference it wherever I want it to appear. So I can create a Views folder and add all the xibs in it. Whenever I want to change a color or a layout, I can do it in this view and the change will reflect among all the instances

Installation

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

pod 'MSAutoView'

Prerequisites

  • XCode 9

Usage

  1. Add a xib and give it any name (in my case it will be "ListingView") Image

  2. Click on the xib

  3. (For convenience) Go to attribute inspector and change the simulated metrics as below:

    Image

  4. Add whatever views you want in the xib (Example):

    Image

  5. Create a Cocoa Touch Class and give it the same name as the xib (in my case it will be "ListingView"), make sure it inherits from MSAutoView

    Image

  6. Go back to the xib and click on the File's Owner in the Document Outline:

    Image

  7. Click on the Identity Inspector and set the class to the cocoa touch class created in step 5 (in my case "ListingView")

    Image

  8. Add a view to your storyboard

  9. Set its class to the class created in step 5

  10. Run the project, the view should contain the content of the xib

    Image

Customizing The View

  • You can add outlets to the class created and connect them in the xib:
class ListingView: MSAutoView {
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var detailsLabel: UILabel!
    @IBOutlet weak var priceLabel: UILabel!
    
}

Image

  • You can also add Inspectable variables and set them from the storyboard (make sure the inspectable variables are not optionals or it won't work):
class ListingView: MSAutoView {
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var detailsLabel: UILabel!
    @IBOutlet weak var priceLabel: UILabel!
    
    @IBInspectable var title: String = "This is a default title"
    @IBInspectable var details: String = "This is a default details"
    @IBInspectable var price: Double = 300
    
    override func awakeFromNib() {
        super.awakeFromNib()
        updateView()
    }
    
    func updateView() {
        titleLabel.text = title
        detailsLabel.text = details
        priceLabel.text = String(describing: price)
        
    }
    
}

Image

  • You can also override the default values from the storyboard's attributes inspector for the view (Make sure you don't set the attributes in the attribute inspector of the xib or they will override all other values):

Image

This is the result:

Image

  • You can also create a reference for the view in the view controller's class and set its values:
class ViewController: UIViewController {

    @IBOutlet weak var listingView: ListingView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        listingView.title = "Title from View Controller"
        listingView.details = "Details from View Controller"
        listingView.price = 40
        listingView.updateView()
    }

}

Image

Deployment

If you want to use the auto view in your own project just copy the AutoView Folder.

Authors

  • Maher Santina - Initial work

License

This project is licensed under the MIT License - see the LICENSE.md file for details

msautoview's People

Contributors

maherksantina avatar

Watchers

James Cloos avatar  avatar Hadi Dbouk 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.