1

enter image description here

I'm trying to implement a dropdown menu in Swift by adding a view below the navigation bar and initially setting it to hidden until a navigationBarItem button is pressed, which works. In the dropdown View I have added two buttons as seen in the code below but it doesn't seem to pick up the event.

var isAnimating: Bool = false
var dropDownViewIsDisplayed : Bool = false
var dropDownView : UIView!

var buttonOne : UIButton!
var buttonTwo : UIButton!

var screenWidth : CGFloat!

@IBOutlet weak var searchNavigationBar: UINavigationItem!

override func viewDidLoad() {
    super.viewDidLoad()

    screenWidth = self.view.bounds.size.width
    self.navigationController?.navigationBar.translucent = false
    dropDownView =  UIView(frame: CGRectMake(0, -15, screenWidth, -80))

    dropDownView.hidden = true
    dropDownView.userInteractionEnabled = true
    self.navigationController?.view.insertSubview(self.dropDownView, belowSubview: (self.navigationController?.navigationBar)!)

    buttonOne = UIButton(frame: CGRectMake(0, 0, screenWidth, 40))
    buttonOne.setTitle("Button One", forState: .Normal)
    buttonOne.setTitleColor(UIColor.blackColor(), forState: .Normal)
    buttonOne.backgroundColor = UIColor.whiteColor()
    buttonOne.addTarget(self, action: Selector("buttonOnePressed"), forControlEvents: UIControlEvents.TouchUpInside)
    buttonOne.userInteractionEnabled = true
    dropDownView.addSubview(buttonOne)

    buttonTwo = UIButton(frame: CGRectMake(0, buttonOne.bounds.size.height, screenWidth, 40))
    buttonTwo.setTitle("Button Two", forState: .Normal)
    buttonTwo.setTitleColor(UIColor.blackColor(), forState: .Normal)
    buttonTwo.backgroundColor = UIColor.whiteColor()
    buttonTwo.addTarget(self, action: Selector("buttonTwoPressed"), forControlEvents: UIControlEvents.TouchUpInside)
    buttonTwo.userInteractionEnabled = true
    dropDownView.addSubview(buttonTwo)

}

func buttonTwoPressed(){
    self.performSegueWithIdentifier("showLocation", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "showLocation") {
        var location: LocationTableViewController = (segue.destinationViewController as? LocationTableViewController)!

    }
}

Button click functions are not being called.

11
  • can you show buttonOnePressed function? Commented Sep 24, 2015 at 14:10
  • Please ensure that you do not have some transparent view on top of UIButton and that action button is not implemented as buttonOnePressed: Commented Sep 24, 2015 at 14:13
  • @bsarr007 updated to show function Commented Sep 24, 2015 at 14:21
  • do you have a visual feedback when you press on the buttons? Commented Sep 24, 2015 at 14:24
  • not at the moment, only using breakpoints and the debugger but the functions aren't being called. Commented Sep 24, 2015 at 14:25

3 Answers 3

1

Are you using Swift 2.0 with Xcode 7? Just by quickly tried playing with your code like this, I found that the event handlers are being called properly without any big changes. Are you sure that there is nothing wrong somewhere else?

var isAnimating: Bool = false
var dropDownViewIsDisplayed : Bool = false
var dropDownView : UIView!

var buttonOne : UIButton!
var buttonTwo : UIButton!

var screenWidth : CGFloat!

@IBOutlet weak var searchNavigationBar: UINavigationItem!

override func viewDidLoad() {
    super.viewDidLoad()

    screenWidth = self.view.bounds.size.width
    self.navigationController?.navigationBar.translucent = false

   // I modified these 2 lines to test your code immediately
    dropDownView =  UIView(frame: CGRectMake(0, 65, screenWidth, 80))        
    dropDownView.hidden = false

    dropDownView.userInteractionEnabled = true
    self.navigationController?.view.insertSubview(self.dropDownView, belowSubview: (self.navigationController?.navigationBar)!)

    buttonOne = UIButton(frame: CGRectMake(0, 0, screenWidth, 40))
    buttonOne.setTitle("Button One", forState: .Normal)
    buttonOne.setTitleColor(UIColor.blackColor(), forState: .Normal)
    buttonOne.backgroundColor = UIColor.whiteColor()
    buttonOne.addTarget(self, action: Selector("buttonOnePressed"), forControlEvents: UIControlEvents.TouchUpInside)
    buttonOne.userInteractionEnabled = true
    dropDownView.addSubview(buttonOne)

    buttonTwo = UIButton(frame: CGRectMake(0, buttonOne.bounds.size.height, screenWidth, 40))
    buttonTwo.setTitle("Button Two", forState: .Normal)
    buttonTwo.setTitleColor(UIColor.blackColor(), forState: .Normal)
    buttonTwo.backgroundColor = UIColor.whiteColor()
    // Here I just wanted to show you that calling Selector() is not necessary at all
    buttonTwo.addTarget(self, action: "buttonTwoPressed", forControlEvents: UIControlEvents.TouchUpInside)
    buttonTwo.userInteractionEnabled = true
    dropDownView.addSubview(buttonTwo)
}

// I didn't see this method in your code above so I added to test and it works!
func buttonOnePressed() {
    print("buttonOnePressed")
}
// This is also being called normally
func buttonTwoPressed() {
    print("buttonTwoPressed")
}
Sign up to request clarification or add additional context in comments.

3 Comments

very odd it doesn't work on mine. Perhaps because the controller is a root controller of a navigation controller in the main story board ?
My view controller is also the root view controller of a navigation controller so it must not be the point. Could you share your code via GitHub?
hey thanks, I've sorted it out now. Had to do with the initial frame it was set to. Thanks for your pointers
0

Can you please try with:

buttonOne.addTarget(self, action: Selector("buttonOnePressed:"), forControlEvents: UIControlEvents.TouchUpInside)

buttonTwo.addTarget(self, action: Selector("buttonTwoPressed:"), forControlEvents: UIControlEvents.TouchUpInside)

2 Comments

After you shared your button handler code, I see you have implemented it correctly. Did u check if there is some transparent view covering buttons?
how do you check for that ? this controller is a root view controller of a navigation controller in the story board.
0

It won't works because it outside self.navigationController?.navigationBar.frame. You should add buttons on self.view or transparent modal view/UIWindow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.