SHARE
TWEET

ViewController

a guest Sep 22nd, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.swift
  3. //  First screen
  4. //
  5. //  Created by karolis on 17/09/2019.
  6. //  Copyright © 2019 karolis. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Foundation
  11.  
  12. class HomeController: UIViewController {
  13.    
  14.     //SEARCH BAR *******
  15.     let searchBar = UISearchBar()
  16.    
  17.     @objc func handleShowSearchBar() {
  18.         searchBar.becomeFirstResponder()
  19.         search(shouldShow: true)
  20.     }
  21.    
  22.     func configureUI() {
  23.         view.backgroundColor = .white
  24.        
  25.         searchBar.sizeToFit()
  26.         searchBar.delegate = self as? UISearchBarDelegate
  27.        
  28.         navigationController?.navigationBar.barTintColor = UIColor(red: 55/255, green: 120/255,
  29.                                                                    blue: 250/255, alpha: 1)
  30.         navigationController?.navigationBar.barStyle = .black
  31.         navigationController?.navigationBar.tintColor = .white
  32.         navigationController?.navigationBar.prefersLargeTitles = true
  33.         navigationController?.navigationBar.isTranslucent = false
  34.         navigationItem.title = "Search Bar"
  35.         showSearchBarButton(shouldShow: true)
  36.     }
  37.    
  38.     func showSearchBarButton(shouldShow: Bool) {
  39.         if shouldShow {
  40.             navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .search,
  41.                                                                 target: self,
  42.                                                                 action: #selector(handleShowSearchBar))
  43.         } else {
  44.             navigationItem.rightBarButtonItem = nil
  45.         }
  46.     }
  47.    
  48.     func search(shouldShow: Bool) {
  49.         showSearchBarButton(shouldShow: !shouldShow)
  50.         searchBar.showsCancelButton = shouldShow
  51.         navigationItem.titleView = shouldShow ? searchBar : nil
  52.     }
  53.    
  54.     //SEARCH BAR *******
  55.    
  56.     //PIRCKER ****
  57.    
  58.     @IBOutlet weak var favoriteDayTextField: UITextField!
  59.    
  60.     let days = ["Pirmas",
  61.                 "Antras",
  62.                 "Trecias",
  63.                 "Ketvirtas",
  64.                 "Penktas",
  65.                 "Sestas",
  66.                 "Septintas"]
  67.    
  68.     var selectedDay: String?
  69.    
  70.     //PICKER ***
  71.    
  72.     let cellId = "cellId"
  73.     let headerId = "headerId"
  74.     let footerId = "footerId"
  75.    
  76.     let collectionView : UICollectionView = {
  77.         let layout = UICollectionViewFlowLayout()
  78.         layout.sectionHeadersPinToVisibleBounds = true
  79.         layout.minimumLineSpacing = 1
  80.         let collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
  81.         collection.translatesAutoresizingMaskIntoConstraints = false
  82.         return collection
  83.     }()
  84.    
  85.     let filterVie: UIView = {
  86.         let view = UIView()
  87.         view.backgroundColor = .gray
  88.         view.translatesAutoresizingMaskIntoConstraints = false
  89.         return view
  90.     }()
  91.    
  92.    
  93.     //searchbar ios swift programmaticaly
  94.    
  95.    
  96.     //filter picker
  97.    
  98.    
  99.     func setupView() {
  100.         view.addSubview(filterVie)
  101.         view.addSubview(collectionView)
  102.         view.addSubview(searchBar)
  103.        
  104.        
  105.        
  106.         filterVie.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
  107.         filterVie.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
  108.         filterVie.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
  109.         filterVie.heightAnchor.constraint(equalToConstant: 50).isActive = true
  110.        
  111.        
  112.         collectionView.topAnchor.constraint(equalTo: filterVie.bottomAnchor).isActive = true
  113.         collectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
  114.         collectionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
  115.         collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  116.        
  117.         searchBar.topAnchor.constraint(equalTo: filterVie.topAnchor).isActive = true
  118.         searchBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
  119.         searchBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
  120.         searchBar.bottomAnchor.constraint(equalTo: filterVie.bottomAnchor).isActive = true
  121.        
  122.        
  123.     }
  124.    
  125. //    //************* PICKER
  126. //    func createDayPicker() {
  127. //
  128. //        let dayPicker = UIPickerView()
  129. //        dayPicker.delegate = self
  130. //
  131. //        favoriteDayTextField!.inputView = dayPicker
  132. //        dayPicker.backgroundColor = .black
  133. //    }
  134. //    func createToolbar() {
  135. //
  136. //        let toolBar = UIToolbar()
  137. //        toolBar.sizeToFit()
  138. //        toolBar.barTintColor = .black
  139. //        toolBar.tintColor = .white
  140. //
  141. //        let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(UIViewController.dismiss(animated:completion:)))
  142. //
  143. //        toolBar.setItems([doneButton], animated: false)
  144. //        toolBar.isUserInteractionEnabled = true
  145. //
  146. //        favoriteDayTextField.inputAccessoryView = toolBar
  147. //    }
  148. //
  149. //    @objc func dismissKeyboard() {
  150. //        view.endEditing(true)
  151. //    }
  152. //    //************* PICKER
  153.    
  154.    
  155.     override func viewDidLoad() {
  156.  
  157.         super.viewDidLoad()
  158.         setupView()
  159.         configureUI()
  160.        
  161.         navigationItem.title = "Paskyros"
  162.        
  163.         collectionView.backgroundColor = .white
  164.         collectionView.dataSource = self
  165.         collectionView.delegate = self
  166.         collectionView.register(WordCell.self, forCellWithReuseIdentifier: cellId)
  167.        
  168.    
  169. //        createDayPicker()
  170. //        createToolbar()
  171.  
  172.     }
  173. }
  174.  
  175. //SEARCH BAR *******
  176. extension HomeController: UISearchBarDelegate {
  177.     func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
  178.         print("Search bar editing did begin..")
  179.     }
  180.    
  181.     func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
  182.         print("Search bar editing did end..")
  183.     }
  184.    
  185.     func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  186.         search(shouldShow: false)
  187.     }
  188.    
  189.     func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  190.         print("Search text is \(searchText)")
  191.     }
  192. }
  193. //SEARCH BAR *******
  194.  
  195.  
  196.  
  197.  
  198. //************* PICKER
  199. extension HomeController: UIPickerViewDelegate, UIPickerViewDataSource {
  200.    
  201.     func numberOfComponents(in pickerView: UIPickerView) -> Int {
  202.         return 1
  203.     }
  204.    
  205.    
  206.     func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  207.         return days.count
  208.     }
  209.    
  210.    
  211.     func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  212.         return days[row]
  213.     }
  214.    
  215.    
  216.     func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  217.        
  218.         selectedDay = days[row]
  219.         favoriteDayTextField.text = selectedDay
  220.     }
  221.     func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
  222.        
  223.         var label: UILabel
  224.        
  225.         if let view = view as? UILabel {
  226.             label = view
  227.         } else {
  228.             label = UILabel()
  229.         }
  230.        
  231.         label.textColor = .green
  232.         label.textAlignment = .center
  233.         label.font = UIFont(name: "Menlo-Regular", size: 17)
  234.        
  235.         label.text = days[row]
  236.        
  237.         return label
  238.     }
  239. }
  240. //************* PICKER
  241.  
  242.  
  243. extension HomeController : UICollectionViewDelegate {
  244.    
  245. }
  246.  
  247.  
  248. extension HomeController : UICollectionViewDataSource {
  249.    
  250.     func numberOfSections(in collectionView: UICollectionView) -> Int {
  251.         return 1
  252.     }
  253.     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  254.         return 7
  255.     }
  256.    
  257.    
  258.    
  259.     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  260.         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! WordCell
  261.        
  262.        
  263.        
  264.         return cell
  265.     }
  266.    
  267.  
  268. }
  269.  
  270.  
  271. extension HomeController : UICollectionViewDelegateFlowLayout  {
  272.    
  273.    
  274.     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  275.         return CGSize(width: collectionView.frame.width, height: 65)
  276.     }
  277. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top