Removing UISegmentedControl through collectionView didSelect

Hi, I have a UISegmentedControl implemented programmatically that I would like to remove from the screen when I select collectionView didSelect. I've tried many solutions but couldn't get success. Any advice on that please ? Sharing the relevant code;


import Foundation
import UIKit
import Photos

class PhotoViewController: UIViewController, UIScrollViewDelegate {

func effectsSegmentedControl() {
    let items = ["Brightness", "Contrast", "Saturation", "Noise"]
    let segmentedControl = UISegmentedControl(items: items)
    segmentedControl.addTarget(self, action: #selector(effectsDidChange(_:)), for: .valueChanged)
    segmentedControl.translatesAutoresizingMaskIntoConstraints = false
  
    view.addSubview(segmentedControl)
  
    NSLayoutConstraint.activate([
      segmentedControl.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30),
      segmentedControl.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30),
      segmentedControl.bottomAnchor.constraint(equalTo: resetConfirmCollectionView.topAnchor, constant: -10)])
  }

  @objc func effectsDidChange(_ segmentedControl: UISegmentedControl) {
    switch segmentedControl.selectedSegmentIndex {
    case 0:
      effectsSliderBrightness()
    case 1:
      effectsSliderContrast()
    //      sliderLabel(myText: "")
    case 2:
      effectsSliderSaturation()
    //      sliderLabel(myText: "")
    case 3:
      effectsSliderNoise()
    //      sliderLabel(myText: "")
    default:
      break
    }
  }

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if collectionView == resetConfirmCollectionView {
      if indexPath.row == 0 {
        //        photoImageView.image = pickedImage
        resetConfirmCollectionView.isHidden = true
        menuStackView.isHidden = false

      }
      if indexPath.row == 1 {
        //        filteredImage = pickedImage
        resetConfirmCollectionView.isHidden = true
        menuStackView.isHidden = false

      }
    }
}

Accepted Reply

I would move the var for UISegmented Control out of the func, so that it is globally visible and pesistent. Otherwise, view remains on screen but you've lost handle to it !


I would also get effectsDidChange out.


I do not see where you try to remove the segmented control. What did you try ?


There are a lot of missing information on your code, so I could only guess.


// Do not see where resetConfirmCollectionView is defined ? Does it contain segmentedControl ?

// There is no viewDidLoad ?

// Where is effectsSegmentedControl called ?


class PhotoViewController: UIViewController, UIScrollViewDelegate {
   
    private var segmentedControl : UISegmentedControl! // Should be set in viewDidLoad
   
    override func viewDidLoad() {
        let items = ["Brightness", "Contrast", "Saturation", "Noise"]
        // let
        segmentedControl = UISegmentedControl(items: items)
    }
   
    @objc func effectsDidChange(_ segmentedControl: UISegmentedControl) {
        switch segmentedControl.selectedSegmentIndex {
        case 0:
            effectsSliderBrightness()
        case 1:
            effectsSliderContrast()
        //      sliderLabel(myText: "")
        case 2:
            effectsSliderSaturation()
        //      sliderLabel(myText: "")
        case 3:
            effectsSliderNoise()
        //      sliderLabel(myText: "")
        default:
            break
        }
    }
   
    func effectsSegmentedControl() {
//        let items = ["Brightness", "Contrast", "Saturation", "Noise"]
//        // let
//        segmentedControl = UISegmentedControl(items: items)
        segmentedControl.addTarget(self, action: #selector(effectsDidChange(_:)), for: .valueChanged)
        segmentedControl.translatesAutoresizingMaskIntoConstraints = false
       
        view.addSubview(segmentedControl)
       
        NSLayoutConstraint.activate([
            segmentedControl.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30),
            segmentedControl.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30),
            segmentedControl.bottomAnchor.constraint(equalTo: resetConfirmCollectionView.topAnchor, constant: -10)])
    }
   

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if collectionView == resetConfirmCollectionView {
            if indexPath.row == 0 {
                //        photoImageView.image = pickedImage
                resetConfirmCollectionView.isHidden = true
                menuStackView.isHidden = false
               
            }
            if indexPath.row == 1 {
                //        filteredImage = pickedImage
                resetConfirmCollectionView.isHidden = true
                segmentedControl.isHidden = true
                menuStackView.isHidden = false
               
            }
        }
    }
}

Replies

I would move the var for UISegmented Control out of the func, so that it is globally visible and pesistent. Otherwise, view remains on screen but you've lost handle to it !


I would also get effectsDidChange out.


I do not see where you try to remove the segmented control. What did you try ?


There are a lot of missing information on your code, so I could only guess.


// Do not see where resetConfirmCollectionView is defined ? Does it contain segmentedControl ?

// There is no viewDidLoad ?

// Where is effectsSegmentedControl called ?


class PhotoViewController: UIViewController, UIScrollViewDelegate {
   
    private var segmentedControl : UISegmentedControl! // Should be set in viewDidLoad
   
    override func viewDidLoad() {
        let items = ["Brightness", "Contrast", "Saturation", "Noise"]
        // let
        segmentedControl = UISegmentedControl(items: items)
    }
   
    @objc func effectsDidChange(_ segmentedControl: UISegmentedControl) {
        switch segmentedControl.selectedSegmentIndex {
        case 0:
            effectsSliderBrightness()
        case 1:
            effectsSliderContrast()
        //      sliderLabel(myText: "")
        case 2:
            effectsSliderSaturation()
        //      sliderLabel(myText: "")
        case 3:
            effectsSliderNoise()
        //      sliderLabel(myText: "")
        default:
            break
        }
    }
   
    func effectsSegmentedControl() {
//        let items = ["Brightness", "Contrast", "Saturation", "Noise"]
//        // let
//        segmentedControl = UISegmentedControl(items: items)
        segmentedControl.addTarget(self, action: #selector(effectsDidChange(_:)), for: .valueChanged)
        segmentedControl.translatesAutoresizingMaskIntoConstraints = false
       
        view.addSubview(segmentedControl)
       
        NSLayoutConstraint.activate([
            segmentedControl.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 30),
            segmentedControl.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -30),
            segmentedControl.bottomAnchor.constraint(equalTo: resetConfirmCollectionView.topAnchor, constant: -10)])
    }
   

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if collectionView == resetConfirmCollectionView {
            if indexPath.row == 0 {
                //        photoImageView.image = pickedImage
                resetConfirmCollectionView.isHidden = true
                menuStackView.isHidden = false
               
            }
            if indexPath.row == 1 {
                //        filteredImage = pickedImage
                resetConfirmCollectionView.isHidden = true
                segmentedControl.isHidden = true
                menuStackView.isHidden = false
               
            }
        }
    }
}

Your guidance solved the problem, thank you very much Claude, have a nice day.

Everything in AtOmXpLuS.CoM