Reputation: 157
I'm having a tough time with this issue. I have a custom UIView, that contains a UIImageView and a UIButton. The custom UIView is called a PPButton, and the code for adding these controls is below. One thing to note is that I am overwriting drawRect, which shouldn't effect what is going on here.
private func addSubviews() {
let buttonFontMultipllier : CGFloat = 4.5 / 10.0
let buttonFontSize = (self.subRect!.height) * buttonFontMultipllier
self.ppButton = UIButton.buttonWithType(UIButtonType.Custom) as? UIButton
self.ppButton!.setTitle(self.ppButtonTitle!, forState: .Normal)
self.ppButton!.setTitleColor(UIColor.whiteColor(), forState: .Normal)
self.ppButton!.titleLabel!.font = UIFont(name: "Formata-BoldCondensed", size: buttonFontSize)
self.ppButton!.contentHorizontalAlignment = .Left
self.ppButton!.addTarget(self, action:"buttonWasTapped:", forControlEvents: UIControlEvents.TouchUpInside)
self.ppButton!.userInteractionEnabled = true
self.ppButton!.backgroundColor = UIColor.greenColor()
self.ppButton!.layer.borderColor = UIColor.greenColor().CGColor
self.ppButton!.layer.borderWidth = 1.0
self.ppImage = UIImageView()
self.ppImage!.contentMode = UIViewContentMode.ScaleAspectFit
self.ppImage!.userInteractionEnabled = false
self.ppImage!.layer.borderColor = UIColor.greenColor().CGColor
self.ppImage!.layer.borderWidth = 1.0
self.ppButtonView!.addSubview(self.ppButton!)
self.ppButtonView!.addSubview(self.ppImage!)
self.ppButtonView!.bringSubviewToFront(self.ppButton!)
self.ppButtonView!.sendSubviewToBack(self.ppImage!)
self.ppButtonView!.userInteractionEnabled = false;
self.addSubview(self.ppButtonView!)
superview!.layer.backgroundColor = UIColor.yellowColor().CGColor
}
These buttons are added to another custom View, which is being added to a custom view controller. The code for adding the PPButtons to the custom HomeView is below.
func addSubviews() {
self.homeLogo = UIImageView()
self.homeLogo!.contentMode = UIViewContentMode.ScaleAspectFit
self.addSubview(self.homeLogo!)
self.orderBtn = PPButton(title: "Order")
self.orderBtn!.delegate = self
self.orderBtn!.userInteractionEnabled = false
self.addSubview(self.orderBtn!)
self.findUsBtn = PPButton(title: "Find Us")
self.findUsBtn!.delegate = self
self.orderBtn!.userInteractionEnabled = false
self.addSubview(self.findUsBtn!)
self.menuBtn = PPButton(title: "Menu")
self.menuBtn!.delegate = self
self.orderBtn!.userInteractionEnabled = false
self.addSubview(self.menuBtn!)
self.clubPatronBtn = PPButton(title: "Club Patron")
self.clubPatronBtn!.delegate = self
self.orderBtn!.userInteractionEnabled = false
self.addSubview(self.clubPatronBtn!)
self.loginButton = UIButton()
self.loginButton!.setTitle(String("Login or Create an Account").uppercaseString, forState: .Normal)
self.loginButton!.setTitleColor(UIColor.whiteColor(), forState: .Normal)
self.loginButton!.titleLabel?.font = UIFont(name: "Formata-BoldCondensed", size: 18.0)
self.loginButton!.userInteractionEnabled = true
self.loginButton!.addTarget(self, action: Selector("testFunc"), forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(self.loginButton!)
}
When I try to click the buttons, the targets aren't firing for the PPButtons. Further, they aren't firing for the loginButton, which is just a simple UIButton. All of these are being added programmatically. I'm sure there is something small I am doing wrong.
Also, I ran a check to see of all my views, starting at the ViewController, which ones has the userInteractionEnabled set to true. The function and output are below. Please help!
func logViewHierarchy(view: UIView, num: Int) {
var index = num
for i in 0..<index {
print("\t")
}
index = index + 1
println("\(NSStringFromClass(view.dynamicType)) userInteractionEnabled: \(view.userInteractionEnabled)")
for subview in view.subviews {
self.logViewHierarchy(subview as! UIView, num: index)
}
}
OUTPUT
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
_UILayoutGuide userInteractionEnabled: true
_UILayoutGuide userInteractionEnabled: true
Pizza_Patron.HomeView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
Pizza_Patron.PPButton userInteractionEnabled: false
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
Pizza_Patron.PPButton userInteractionEnabled: true
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
Pizza_Patron.PPButton userInteractionEnabled: true
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
Pizza_Patron.PPButton userInteractionEnabled: true
UIView userInteractionEnabled: false
UIImageView userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
UIButton userInteractionEnabled: true
UIButtonLabel userInteractionEnabled: false
Upvotes: 0
Views: 3031
Reputation: 724
So it looks like your top level view as well as a few ones lower down have userInteractionEnabled
set to false
. This cascades down so if a superview has interaction turned off, its subviews won't receive interaction events.
You could reorder your hierarchy if you want to keep some of those view's interactions disabled, just turn interaction on for those superviews or try something like this and pass through the events:
How to get touches when parent view has userInteractionEnabled set to NO in iOS
Upvotes: 3