I have two UIButton
'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.
Code:
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() {
super.viewDidLoad()
initButtons()
}
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)
button_view.addSubview(mylists)
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)
button_view.addSubview(settings)
self.view.addSubview(button_view)
}
func showLists(sender: UIButton!) {
print("showLists")
}
func showSettings(sender: UIButton!) {
print("showSettings")
}
Any help is appreciated. Thanks!