Dando18 Dando18 - 1 year ago 78
iOS Question

Offscreen UIButton action not triggered - iOS

I have two

's created off screen that are animated onto the screen when a button is pressed. However, neither of the button's target functions are never called, as I believe the buttons are not being "pressed". The buttons do work if I simply create them on the screen, so I believe the issue is related to the buttons being animated onto the screen from outside the screens bounds.

Edit: The buttons do animate onto the screen properly and are visible, however pressing them does nothing.


var isMenuOpen : Bool = false
var button_height : CGFloat = UIScreen.mainScreen().bounds.height/8

var button_view : UIView!
var mylists : UIButton!
var settings : UIButton!

@IBAction func openMenu(sender: AnyObject) {
UIView.animateWithDuration(0.6, delay: 0.0, options: .CurveEaseOut, animations: {
self.view.transform = CGAffineTransformMakeTranslation(0.0, self.button_height * (self.isMenuOpen ? 0 : -1))
}, completion: {finished in
self.isMenuOpen = !self.isMenuOpen


override func viewDidLoad() {

func initButtons() {
button_view = UIView(frame: CGRect(x:0.0, y:UIScreen.mainScreen().bounds.height, width:UIScreen.mainScreen().bounds.width, height:button_height))
button_view.userInteractionEnabled = true

mylists = UIButton(frame: CGRect(x:0.0, y:0.0, width:UIScreen.mainScreen().bounds.width/2.0, height:button_height))
mylists.setTitle("My Lists", forState: .Normal)
mylists.backgroundColor = UIColor(red: 255/255, green: 105/255, blue: 180/255, alpha: 1.0)
mylists.addTarget(self, action: #selector(showLists(_:)), forControlEvents: .TouchUpInside)

settings = UIButton(frame: CGRect(x:UIScreen.mainScreen().bounds.width/2.0, y:0.0, width:UIScreen.mainScreen().bounds.width/2.0, height:button_height))
settings.setTitle("Settings", forState: .Normal)
settings.backgroundColor = UIColor(red: 135/255, green: 205/255, blue: 236/255, alpha: 1.0)
settings.addTarget(self, action: #selector(showSettings(_:)), forControlEvents: .TouchUpInside)


func showLists(sender: UIButton!) {

func showSettings(sender: UIButton!) {

Any help is appreciated. Thanks!

Answer Source

I don't the exact goal of your transition, but it looks like the view that is holding the buttons button_view is never being transition onto the screen.

Are you trying to make each button go to the screen or are you trying to make both buttons go to the screen inside a container (button_view_)? If you are trying to do both, I would recommend doing

UIView.animateWithDuration(0.6, delay: 0.0, options: .CurveEaseOut, animations: {
    self.button_view.transform = CGAffineTransformMakeTranslation(0.0, -1*(UIScreen.mainScreen().bounds.height))
}, completion: {finished in
    self.isMenuOpen = !self.isMenuOpen

This is based off the assumption you are trying to move the box to the screen as a whole.


Ahh! I see what is happening! So you are moving self.view up, which you never wanna move a UIViewController.view. Move a container of the view. So instead of moving self.view, move button_view. Moving a UIController.view removes all of the underneath layers and functionality of a UIViewController

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download