【发布时间】:2015-07-30 21:44:42
【问题描述】:
我很难解决这个问题。我有一个自定义 UIView,它包含一个 UIImageView 和一个 UIButton。自定义的 UIView 称为 PPButton,添加这些控件的代码如下。需要注意的一件事是我正在覆盖 drawRect,这不应该影响这里发生的事情。
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
}
这些按钮被添加到另一个自定义视图中,该视图被添加到自定义视图控制器中。将 PPButtons 添加到自定义 HomeView 的代码如下。
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!)
}
当我尝试单击按钮时,目标不会触发 PPButton。此外,它们不会触发 loginButton,它只是一个简单的 UIButton。所有这些都是以编程方式添加的。我确定我做错了一些小事。
另外,我从 ViewController 开始检查我的所有视图,哪些视图的 userInteractionEnabled 设置为 true。函数和输出如下。请帮忙!
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)
}
}
输出
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
【问题讨论】:
标签: ios iphone xcode uiview uibutton