Quantcast
Channel: Active questions tagged uipickerview - Stack Overflow
Viewing all articles
Browse latest Browse all 592

Swift 5: UIPickerView dismisses slide-in panel

$
0
0

I have a picker view implemented on a view that is out of sight by default and slides-in when button is pressed.

Below is my code and screenshot:

class PromotionViewController: UIViewController {    //MARK:- Declarations    // IBOutlets    @IBOutlet weak var promotionTableView: UITableView!    @IBOutlet weak var promotionBannerCollectionView: UICollectionView!    @IBOutlet weak var filterView: UIView!    @IBOutlet weak var categoryTextField: UITextField!    @IBOutlet weak var filterApplyButton: UIButton!    @IBOutlet weak var filterResetButton: UIButton!    @IBOutlet weak var overlayView: UIView!    // Objects    var promotionArray = [PromotionModel]()    var promotionBannerArray = [BannerModel]()    var searchingPromotionArray = [PromotionModel]()    // Misc    var overlay = UIView()    var overlay2 = UIView()    var searchBar = UISearchBar()    var noPromotionsFound = UILabel()    var backButton = UIBarButtonItem()    var filterButton = UIBarButtonItem()    var picker = UIPickerView()    var searching = 0    var selectedPromotionID = ""    var isFiltering = false    var promoCategoryArray = [Int]()    var promoCategoryArrayRemovedDupes = [Int]()    var promoCategoryArraySorted = [Int]()    //MARK:- Functions (Override)    override func viewDidLoad() {        super.viewDidLoad()        // Setup        setupFilter()        setupOverlay()        setupTables()        setupTapGesture()        setupNavigationBar()        setupDelegateDataSource()    }    // Segue    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {        if segue.identifier == "promotionToPromotionDetails" {            if let nextViewController = segue.destination as? PromotionDetailsViewController {                nextViewController.selectedPromotionSwift = selectedPromotionID            }        }    }    //MARK:- Functions (Setup)    func setupDelegateDataSource() {        // Assign        picker.delegate = self        searchBar.delegate = self        promotionTableView.delegate = self        promotionTableView.dataSource = self        promotionBannerCollectionView.delegate = self        promotionBannerCollectionView.dataSource = self    }    func setupOverlay() {        overlayView.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.5)        overlayView.isHidden = true    }    func setupTapGesture() {        //Declare        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))        // Assign        view.addGestureRecognizer(tap)    }    func setupFilter() {        categoryTextField.rightViewMode = UITextField.ViewMode.always        categoryTextField.rightView = UIImageView(image: #imageLiteral(resourceName: "navi-right"))        filterApplyButton.layer.cornerRadius = 15        filterResetButton.layer.cornerRadius = 15        categoryTextField.inputView = picker    }    func setupNavigationBar() {        // Declare        backButton = UIBarButtonItem(image: #imageLiteral(resourceName: "icon-back").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(dismissController))        filterButton = UIBarButtonItem(image: #imageLiteral(resourceName: "icon-filter").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(filterButtonPressed))        // Assign        self.navigationItem.titleView = searchBar        self.navigationItem.leftBarButtonItem = backButton        self.navigationItem.rightBarButtonItem = filterButton    }    func setupTables() {        // Declare        promotionTableView.tableFooterView = UIView()        promotionTableView.register(UINib(nibName: "PromotionCell", bundle: nil), forCellReuseIdentifier: "promotionCell")        promotionBannerCollectionView.register(UINib(nibName: "PromotionBannerCell", bundle: nil), forCellWithReuseIdentifier: "promotionBannerCell")        // Assign        noPromotionsFound.text = "No Promotions Found."        self.promotionTableView.keyboardDismissMode = .onDrag        self.promotionBannerCollectionView.keyboardDismissMode = .onDrag        // Design        noPromotionsFound.textAlignment = .center        noPromotionsFound.textColor = .darkGray        promotionTableView.estimatedRowHeight = 100        promotionTableView.rowHeight = UITableView.automaticDimension    }    //MARK:- Functions (Misc)    @objc func filterButtonPressed() {        if isFiltering == false {            UIView.animate(withDuration: 0.4, delay: 0.0, options: .curveEaseIn, animations: {                self.filterView.frame.origin.x = self.view.frame.size.width/3                self.filterView.frame = self.filterView.frame                self.overlayView.isHidden = false            }) { (completed) in            }            isFiltering = true        } else {            UIView.animate(withDuration: 0.4, delay: 0.0, options: .curveEaseIn, animations: {                self.filterView.frame.origin.x = self.view.frame.size.width                self.filterView.frame = self.filterView.frame                self.overlayView.isHidden = true            }) { (completed) in            }            isFiltering = false            overlayView.isHidden = true        }    }    @objc func dismissController() {        dismiss(animated: true, completion: nil)    }    @objc func dismissKeyboard() {        view.endEditing(true)    }    @IBAction func filterBackButtonPressed(_ sender: UIButton) {        filterButtonPressed()    }    func sortCategory() {        self.promoCategoryArrayRemovedDupes = Array(Set(self.promoCategoryArray))        self.promoCategoryArraySorted = self.promoCategoryArrayRemovedDupes.sorted()    }}//MARK:- Extension (Promotion Table View)extension PromotionViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {        return promotionBannerArray.count    }    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {        return CGSize(width: promotionBannerCollectionView.frame.width, height: promotionBannerCollectionView.frame.height)    }    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {        // Declare        let cell = promotionBannerCollectionView.dequeueReusableCell(withReuseIdentifier: "promotionBannerCell", for: indexPath) as! PromotionBannerCell        let promotionImageUrl = NSURL(string: promotionBannerArray[indexPath.row].image?.s3ImageURL ?? "https://www.teknozeka.com/wp-content/uploads/2020/03/wp-header-logo-28.png")        let promotionImage = NSData(contentsOf: promotionImageUrl! as URL)        // Assign        cell.promotionBannerImageView.image = UIImage(data: promotionImage! as Data)        // Design        cell.contentView.layer.cornerRadius = 10        cell.contentView.backgroundColor = .white        cell.layer.shadowColor = UIColor.lightGray.cgColor        cell.layer.shadowOffset = CGSize(width: 0, height: 1)        cell.layer.shadowRadius = 3.0        cell.layer.shadowOpacity = 0.5        cell.layer.masksToBounds = false        cell.layer.backgroundColor = UIColor.clear.cgColor        cell.promotionBannerImageView.widthAnchor.constraint(equalToConstant: promotionBannerCollectionView.frame.width).isActive = true        cell.promotionBannerImageView.contentMode = .scaleAspectFit        return cell    }}//MARK:- Extension (Promotion)extension PromotionViewController: UITableViewDelegate, UITableViewDataSource {    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {        return searchingPromotionArray.count    }    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {        // Declare        let cell = promotionTableView.dequeueReusableCell(withIdentifier: "promotionCell", for: indexPath) as! PromotionCell        let promo = searchingPromotionArray[indexPath.row]        let promotionImageUrl = NSURL(string: searchingPromotionArray[indexPath.row].images[0].s3ImageURL)        let promotionImage = NSData(contentsOf: promotionImageUrl! as URL)        // Assign        cell.promotionNameLabel.text = promo.name        cell.promotionDescriptionLabel.text = "Promotions Starts: \(promo.promotionStart)"        cell.promotionImageView?.image = UIImage(data: promotionImage! as Data)        // Design        return cell    }    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {        selectedPromotionID = String(searchingPromotionArray[indexPath.row].id)        performSegue(withIdentifier: "promotionToPromotionDetails", sender: self)    }}//MARK:- Extension (Search Bar)extension PromotionViewController: UISearchBarDelegate {    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {        // Declare        searchingPromotionArray = promotionArray.filter({$0.name.lowercased().contains(searchBar.text!.lowercased())})        searching = 1        // Design        promotionTableView.backgroundView = nil        if searchingPromotionArray.isEmpty {            promotionTableView.backgroundView = noPromotionsFound        }        if searchBar.text!.isEmpty {            searching = 0            // Assign            searchingPromotionArray = promotionArray        }        // Calling        promotionTableView.reloadData()    }    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {        searchBar.resignFirstResponder()    }}//MARK:- Extension (Picker View)extension PromotionViewController: UIPickerViewDelegate, UIPickerViewDataSource {    func numberOfComponents(in pickerView: UIPickerView) -> Int {        return 1    }    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {        sortCategory()        return promoCategoryArraySorted.count    }    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {        sortCategory()        return "#\(promoCategoryArraySorted[row])"    }    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {        sortCategory()        categoryTextField.text = "#\(promoCategoryArraySorted[row])"    }}

enter image description here

The issue: the view is dismissed and slides off screen once an option has been picked from the picker view. Ideally it should stay put once user is done and the categoryTextField.text will be set to whatever that was picked.

PS: Content censored for privacy/confidentiality purposes. Thank you for your help in advance.


Viewing all articles
Browse latest Browse all 592

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>