Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // First screen
- //
- // Created by karolis on 17/09/2019.
- // Copyright © 2019 karolis. All rights reserved.
- //
- import UIKit
- import Foundation
- class HomeController: UIViewController {
- //SEARCH BAR *******
- let searchBar = UISearchBar()
- @objc func handleShowSearchBar() {
- searchBar.becomeFirstResponder()
- search(shouldShow: true)
- }
- func configureUI() {
- view.backgroundColor = .white
- searchBar.sizeToFit()
- searchBar.delegate = self as? UISearchBarDelegate
- navigationController?.navigationBar.barTintColor = UIColor(red: 55/255, green: 120/255,
- blue: 250/255, alpha: 1)
- navigationController?.navigationBar.barStyle = .black
- navigationController?.navigationBar.tintColor = .white
- navigationController?.navigationBar.prefersLargeTitles = true
- navigationController?.navigationBar.isTranslucent = false
- navigationItem.title = "Search Bar"
- showSearchBarButton(shouldShow: true)
- }
- func showSearchBarButton(shouldShow: Bool) {
- if shouldShow {
- navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .search,
- target: self,
- action: #selector(handleShowSearchBar))
- } else {
- navigationItem.rightBarButtonItem = nil
- }
- }
- func search(shouldShow: Bool) {
- showSearchBarButton(shouldShow: !shouldShow)
- searchBar.showsCancelButton = shouldShow
- navigationItem.titleView = shouldShow ? searchBar : nil
- }
- //SEARCH BAR *******
- //PIRCKER ****
- @IBOutlet weak var favoriteDayTextField: UITextField!
- let days = ["Pirmas",
- "Antras",
- "Trecias",
- "Ketvirtas",
- "Penktas",
- "Sestas",
- "Septintas"]
- var selectedDay: String?
- //PICKER ***
- let cellId = "cellId"
- let headerId = "headerId"
- let footerId = "footerId"
- let collectionView : UICollectionView = {
- let layout = UICollectionViewFlowLayout()
- layout.sectionHeadersPinToVisibleBounds = true
- layout.minimumLineSpacing = 1
- let collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
- collection.translatesAutoresizingMaskIntoConstraints = false
- return collection
- }()
- let filterVie: UIView = {
- let view = UIView()
- view.backgroundColor = .gray
- view.translatesAutoresizingMaskIntoConstraints = false
- return view
- }()
- //searchbar ios swift programmaticaly
- //filter picker
- func setupView() {
- view.addSubview(filterVie)
- view.addSubview(collectionView)
- view.addSubview(searchBar)
- filterVie.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
- filterVie.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
- filterVie.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
- filterVie.heightAnchor.constraint(equalToConstant: 50).isActive = true
- collectionView.topAnchor.constraint(equalTo: filterVie.bottomAnchor).isActive = true
- collectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
- collectionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
- collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
- searchBar.topAnchor.constraint(equalTo: filterVie.topAnchor).isActive = true
- searchBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
- searchBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
- searchBar.bottomAnchor.constraint(equalTo: filterVie.bottomAnchor).isActive = true
- }
- // //************* PICKER
- // func createDayPicker() {
- //
- // let dayPicker = UIPickerView()
- // dayPicker.delegate = self
- //
- // favoriteDayTextField!.inputView = dayPicker
- // dayPicker.backgroundColor = .black
- // }
- // func createToolbar() {
- //
- // let toolBar = UIToolbar()
- // toolBar.sizeToFit()
- // toolBar.barTintColor = .black
- // toolBar.tintColor = .white
- //
- // let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(UIViewController.dismiss(animated:completion:)))
- //
- // toolBar.setItems([doneButton], animated: false)
- // toolBar.isUserInteractionEnabled = true
- //
- // favoriteDayTextField.inputAccessoryView = toolBar
- // }
- //
- // @objc func dismissKeyboard() {
- // view.endEditing(true)
- // }
- // //************* PICKER
- override func viewDidLoad() {
- super.viewDidLoad()
- setupView()
- configureUI()
- navigationItem.title = "Paskyros"
- collectionView.backgroundColor = .white
- collectionView.dataSource = self
- collectionView.delegate = self
- collectionView.register(WordCell.self, forCellWithReuseIdentifier: cellId)
- // createDayPicker()
- // createToolbar()
- }
- }
- //SEARCH BAR *******
- extension HomeController: UISearchBarDelegate {
- func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
- print("Search bar editing did begin..")
- }
- func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
- print("Search bar editing did end..")
- }
- func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
- search(shouldShow: false)
- }
- func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
- print("Search text is \(searchText)")
- }
- }
- //SEARCH BAR *******
- //************* PICKER
- extension HomeController: UIPickerViewDelegate, UIPickerViewDataSource {
- func numberOfComponents(in pickerView: UIPickerView) -> Int {
- return 1
- }
- func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
- return days.count
- }
- func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
- return days[row]
- }
- func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
- selectedDay = days[row]
- favoriteDayTextField.text = selectedDay
- }
- func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
- var label: UILabel
- if let view = view as? UILabel {
- label = view
- } else {
- label = UILabel()
- }
- label.textColor = .green
- label.textAlignment = .center
- label.font = UIFont(name: "Menlo-Regular", size: 17)
- label.text = days[row]
- return label
- }
- }
- //************* PICKER
- extension HomeController : UICollectionViewDelegate {
- }
- extension HomeController : UICollectionViewDataSource {
- func numberOfSections(in collectionView: UICollectionView) -> Int {
- return 1
- }
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return 7
- }
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! WordCell
- return cell
- }
- }
- extension HomeController : UICollectionViewDelegateFlowLayout {
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- return CGSize(width: collectionView.frame.width, height: 65)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement