Giter Site home page Giter Site logo

swift-programmatically's Introduction

swift-programmatically

swift programmatically without storyboard.

This repo is under GPL LICENSE.

This repo was inspired by lanqy/swift-programmatically, but add some changes to suit swift 3 and other swift and refering library changes.

I am focusing on making effective ViewController.

Basis

I provide executable Controller named ViewController, to use this, just follow the under steps:

  1. Delete Main.storyboard
  2. Choose NONE in General - Deployment info - Main Interface
  3. Just modify your AppDelegate like this:
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = ViewController()
        window?.makeKeyAndVisible()
        return true
    }

UITabBarController demo (like the buttom of App Store)

import UIKit
class ViewController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let item1 = Item1ViewController()
        //actually we do not have the image, so the demo image is not visible
        let icon1 = UITabBarItem(title: "item1", image: UIImage(named: "someImage.png"), selectedImage: UIImage(named: "otherImage.png"))
        item1.tabBarItem = icon1
        
        let item2 = Item2ViewController()
        let icon2 = UITabBarItem(title: "item2", image: UIImage(named: "someImage.png"), selectedImage: UIImage(named: "otherImage.png"))
        item2.tabBarItem = icon2
        
        let controllers = [item1,item2]
        self.viewControllers = controllers
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

class Item1ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.green
        self.title = "item1"
        print("item 1 loaded")
    }
}

class Item2ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.red
        self.title = "item2"
        print("item 2 loaded")
    }
}

Effective NavigationController (implements a multi-level nav controller)

import UIKit

class ViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let item1 = Item1ViewController()
        let item2 = Item2ViewController()
        let item3 = Item3ViewController()
        let controllers = [item1,item2,item3]
        self.viewControllers=controllers
    }
}

class Item1ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.green
        self.title = "item1"
        print("item 1 loaded")
    }
}

class Item2ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.red
        self.title = "item2"
        print("item 2 loaded")
    }
}

class Item3ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.black
        self.title = "item3"
        print("item 3 loaded")
    }
}

SearchBar in NavigationBar (combined with a Navigation page)

import UIKit

class ViewController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let item1 = Item1ViewController()
        let item2 = Item2ViewController()
        let controllers = [item1,item2]
        self.viewControllers=controllers
    }
}

class Item1ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.green
        self.title = "item1"
        print("item 1 loaded")
    }
}

class Item2ViewController: UIViewController, UISearchControllerDelegate, UISearchBarDelegate,UISearchResultsUpdating {
    
    var searchController : UISearchController!

    func updateSearchResults( for searchController: UISearchController) {
        print(searchController.searchBar.text ?? "ERROR")
    }

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.searchController = UISearchController(searchResultsController:  nil)
        
        self.searchController.searchResultsUpdater = self
        self.searchController.delegate = self
        self.searchController.searchBar.delegate = self
        
        self.searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.dimsBackgroundDuringPresentation = true
        
        self.navigationItem.titleView = searchController.searchBar
        
        self.definesPresentationContext = true
    }
}

swift-programmatically's People

Contributors

qiukeren avatar

Watchers

James Cloos 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.