skip to Main Content

I have UIButton called menuButton which has a customClass where it sets multiple images and I have a dropDown menu when we click on it. So based on menu selected I want to menuButton to change the image.

This is my code:

public enum MenuItems: String {
  case menu1 = "menu1"
  case menu2 = "menu2"
  case menu3 = "menu3"
}

func layoutView() {
     menuButton.setImages(normalImage: "triangleImage",
                                  image: "menu1",
                                  selectedImage: "closeImage")
    menuButton.addTarget(self, action: #selector(menuItemTapped), for: .touchUpInside)
    self.addSubview(menuButton)
}

func menuItemTapped(sender: UIView) {
    guard let title = sender.accessibilityIdentifier else { return }
    let style = MenuItems(rawValue: title)
    
    switch style {
    case .menu1:
      menuButton.setImages(normalImage: "triangleImage",
                                  image: "menu1",
                                  selectedImage: "closeImage")
    case .menu2:
       menuButton.setImages(normalImage: "triangleImage",
                                  image: "menu2",
                                  selectedImage: "closeImage")
    case .menu3:
       menuButton.setImages(normalImage: "triangleImage",
                                  image: "menu3",
                                  selectedImage: "closeImage")
    default:
      break
    }
}

class MultipleImageButton: UIButton {

  override init(frame: CGRect) {
    super.init(frame: frame)
  }

  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  func setImages(normalImage: UIImage, image: UIImage, selectedImage: UIImage) {
    setImage(normalImage, for: .normal)
    setImage(selectedImage, for: .selected)
    imageEdgeInsets = UIEdgeInsets(top: 0_5x, left: 1x, bottom: 0_5x, right: (bounds.maxX - 55))
    contentHorizontalAlignment = .right


for subview in self.subviews where subview.isKind(of: UIImageView.self) {
            subview.removeFromSuperview()//removes images if any
  }
        let imageView = UIImageView(frame: CGRect(x: bounds.origin.x + 4x,
                                                  y: (titleLabel?.bounds.midY ?? 0) + 1x,
                                                      width: 5x,
                                                  height: frame.height - 2_5x))
    
        imageView.image?.withRenderingMode(.alwaysOriginal)
        imageView.image = image
        imageView.contentMode = .scaleAspectFit
        imageView.layer.masksToBounds = true
        addSubview(imageView)
      }
    }

Using the above code I’m able to change the button image(i.e. imageView) but its not adding the selectedImage and normalImage only the image is getting changed. How do I get the selectedImage and normalImage to display as well?
This is what I need

This is the problem now, which doesn’t display triangle image.

2

Answers


  1. Follow these steps with a simple UIButton; it is not needed to use a custom Button class.

    When clicking menuButton, set the button image to closeImage and show the dropdown:

    menuButton.setImage(UIImage(named: "closeImage"), for: .normal)
    // show Dropdown
    

    When you select the menu item, change the menuButton image and hide the dropdown:

    menuButton.setImage(UIImage(named: "selectedItem"), for: .normal)
    //hide dropdown
    
    Login or Signup to reply.
  2. How do I clear imageView of UIButton in swift?

    Answer: button.(UIImage(), for: .normal)

    Its Simply remove your image from button

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search