xcode 单击时,Swift,自定义 UIButton 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35317796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift, Custom UIButton does not work when click
提问by kakajan
I have a custom UIButton created with xib file. When i use my custom button on my view, it does not work when i press to it.
我有一个用 xib 文件创建的自定义 UIButton。当我在我的视图上使用我的自定义按钮时,当我按下它时它不起作用。
My RoundBtn.swift file:
我的 RoundBtn.swift 文件:
import UIKit
@IBDesignable class RoundBtn: UIButton {
var nibName = "RoundBtn"
@IBOutlet weak var btnImageView: UIImageView!
@IBOutlet weak var btnLabel: UILabel!
@IBInspectable var image: UIImage? {
get {
return btnImageView.image
} set(image) {
btnImageView.image = image
}
}
@IBInspectable var label: String? {
get {
return btnLabel.text
} set(label) {
btnLabel.text = label
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
let view = loadViewFromNib()
view.frame = self.bounds
view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
btnImageView.layer.cornerRadius = 60/2
btnImageView.layer.borderColor = UIColor(red: 5/255,
green: 66/255, blue: 38/255, alpha: 1).CGColor
btnImageView.layer.borderWidth = 2
btnLabel.font = UIFont.boldSystemFontOfSize(14.0)
btnImageView.userInteractionEnabled = true
btnLabel.userInteractionEnabled = true
view.userInteractionEnabled = true
addSubview(view)
}
func loadViewFromNib() -> UIButton {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: nibName, bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIButton
return view
}
}
My RoundBtn.xib file:
我的 RoundBtn.xib 文件:
View where i used my custom button:
查看我在哪里使用我的自定义按钮:
I enabled userInteractionEnabled
on all view components. When i click to my custom button, it does not work. I tried by defining on click programmatically and by defining action segue (show).
我启用userInteractionEnabled
了所有视图组件。当我点击我的自定义按钮时,它不起作用。我尝试通过以编程方式定义点击并定义动作转场(显示)。
@IBAction func myCartBtnPressed(sender: AnyObject) {
print("my cart btn pressed")
}
回答by fdiaz
I think this is caused because you're adding a couple of UIViews subviews to your current UIButton with userInteractionEnabled, which means that they're handling the user input.
我认为这是因为您使用 userInteractionEnabled 向当前的 UIButton 添加了几个 UIViews 子视图,这意味着它们正在处理用户输入。
If you do:
如果你这样做:
view.isUserInteractionEnabled = false
The RoundBtn itself will get all the touch events, instead of this UIViews that you have on top.
RoundBtn 本身将获得所有触摸事件,而不是您在顶部的 UIViews。
回答by Stéphane de Luca
Referring to @fdiaz's answer. Swift 4 notation.
参考@fdiaz 的回答。Swift 4 表示法。
Conversely, if you attach some UIButton (or user interactive view) to an UIImageView, you must turn its isUserInteractionEnabled = true to let the touches go through the hierarchy up down to the UIButton.
相反,如果您将一些 UIButton(或用户交互视图)附加到 UIImageView,则必须将其 isUserInteractionEnabled = true 设置为让触摸通过层次结构向下到达 UIButton。