swift - 以编程方式生成的 UIButton 无响应
问题描述
我正在动态生成 UIButtons 并将它们放置在动态大小的 UIImageView 中。我在一个循环中创建了按钮,然后在另一个循环中我根据可用空间设置了它们的约束。这一切都有效,即按钮和它们的文本显示得很好,但点击它们时什么也没有发生。button.isUserinteractionIsEnables = 是。
当通过最终循环 I 记录按钮时,它们没有 Y,X 坐标。我认为我的主播会处理好这件事。还是将 CGRect 与锚点结合起来总是一个坏主意?
编辑:修复 了按钮的父类初始化我必须设置 .isUserInteractionEnabled = true; 如果在任何时候,视图禁用了用户交互,那么它将拒绝执行其标准操作,包括不将事件传递给它的任何子视图
import UIKit
class SuggenstionCloud: UIImageView {
override init(image: UIImage?) {
super.init(image: image)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public func setConstraints(
topAnchor : NSLayoutAnchor<NSLayoutYAxisAnchor>, topConstant: CGFloat,
bottomAnchor: NSLayoutAnchor<NSLayoutYAxisAnchor>, bottomConstant: CGFloat,
trailingAnchor: NSLayoutAnchor<NSLayoutXAxisAnchor>, trailingConstant: CGFloat,
leadingAnchor: NSLayoutAnchor<NSLayoutXAxisAnchor>, leadingConstant: CGFloat)
{
self.isUserInteractionEnabled = true
self.contentMode = .scaleToFill
self.topAnchor.constraint(equalTo: topAnchor, constant: topConstant).isActive = true;
self.bottomAnchor.constraint(equalTo: bottomAnchor, constant: bottomConstant).isActive = true;
self.trailingAnchor.constraint(equalTo: trailingAnchor, constant: trailingConstant).isActive = true;
self.leadingAnchor.constraint(equalTo: leadingAnchor, constant: leadingConstant).isActive = true;
}
public func setLabels(weightedTags: [String: Int], selectedTags: [String]) {
let buttons : [UIButton] = createButtons(weightedTags: weightedTags);
createLayout(buttons: buttons)
}
private func createButton(buttonText: String) -> UIButton {
let button = UIButton()
button.setTitle(buttonText, for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont(name: "Avenir-Light", size: 20.0)
button.translatesAutoresizingMaskIntoConstraints = false;
self.addSubview(button)
button.frame = CGRect(x:0,y:0,width:button.intrinsicContentSize.width, height: button.intrinsicContentSize.height)
button.addTarget(self, action: #selector(self.onButtonPresed(_:)), for: .touchUpInside);
return button;
}
@objc func onButtonPresed(_ sender: UIButton) {
// guard let label = sender.titleLabel else {return}
print("Button : \(sender) poressed")
}
private func createButtons(weightedTags: [String: Int]) -> [UIButton] {
var buttons : [UIButton] = [];
for tag in weightedTags {
buttons.append(createButton(buttonText: tag.key))
}
return buttons;
}
private func createLayout(buttons : [UIButton]) {
if buttons.count == 0 { return }
let topPadding : CGFloat = 30;
let sidePadding : CGFloat = 32;
let padding : CGFloat = 10;
let availableHeight : CGFloat = self.frame.height + (-2 * topPadding)
let availableWidth : CGFloat = self.frame.width + (-2 * sidePadding)
var i = 0;
var totalHeight : CGFloat = topPadding
var rowLength : CGFloat = 0;
var rowCount : Int = 0;
var lastButton : UIButton!
for button in buttons {
if totalHeight > availableHeight { print("Cloud out of space"); return }
let buttonWidth = button.intrinsicContentSize.width;
let buttonHeight = button.intrinsicContentSize.height;
if rowLength == 0 && rowCount == 0
{
print("FirstButtonLabel \(String(button.titleLabel!.text!))")
setFirstButtonConstraint(button: button, totalHeight: totalHeight, sidePadding: sidePadding)
rowLength += buttonWidth + sidePadding + 5; // FIX annoying first row image overlap
}
else if rowLength + buttonWidth + padding < availableWidth
{
setConstraint(button: button, lastButton: lastButton, totalHeight: totalHeight, padding: padding)
rowLength += buttonWidth + padding;
}
else
{
print("Out of space")
totalHeight += buttonHeight + padding
rowLength = buttonWidth + sidePadding;
rowCount += 1;
setNewRowConstraint(button: button, totalHeight:totalHeight , sidePadding: sidePadding)
}
i += 1;
lastButton = button
print(button.isUserInteractionEnabled)
print(button.allTargets)
print(button)
}
}
private func setFirstButtonConstraint(button: UIButton, totalHeight: CGFloat, sidePadding: CGFloat) {
button.topAnchor.constraint(equalTo: self.topAnchor, constant: totalHeight).isActive = true;
button.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: sidePadding + 5).isActive = true;
}
private func setConstraint(button: UIButton, lastButton: UIButton, totalHeight: CGFloat, padding:CGFloat) {
button.leadingAnchor.constraint(equalTo: lastButton.trailingAnchor, constant: padding).isActive = true;
button.topAnchor.constraint(equalTo: self.topAnchor, constant: totalHeight).isActive = true;
}
private func setNewRowConstraint(button: UIButton, totalHeight: CGFloat, sidePadding: CGFloat) {
button.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: sidePadding).isActive = true;
button.topAnchor.constraint(equalTo: self.topAnchor, constant: totalHeight).isActive = true;
}
}
Print 语句提供以下输出:
true
[AnyHashable(<unFatMobile.SuggenstionCloud: 0x7fb5fae028e0; baseClass = UIImageView; frame = (10 280; 375 317); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x6000005de920>>)]
<UIButton: 0x7fb5fac2d840; frame = (0 0; 41 40); opaque = NO; layer = <CALayer: 0x6000005d0fe0>>
我想我可能对类生命周期方法做错了。
解决方案
我看到评论中已经回答了这个问题,但有一点背景。
当用户点击屏幕时,获取事件的是顶层视图。如果它的 isUserInteractionEnabled 设置为 true,那么它将执行其标准操作(每个 UIView 可以在点击时执行不同的操作。)基本 UIView 的标准操作是确定在将事件传递给该子视图时点击了哪个子视图。通过这种方式,点击事件沿着视图的层次结构从最一般到最具体。
如果在任何时候,视图禁用了用户交互,那么它将拒绝执行其标准操作,包括不将事件传递给它的任何子视图。
另一种意外地使 UIButton 不可粘贴的方法是将其绘制在其父视图的边界之外。视图仅将事件传递给在其边界内绘制的子视图。
推荐阅读
- azure-devops - 使用自定义函数应用编辑模式从 ARM 创建 Azure 函数
- ruby-on-rails - CircleCI 2.0 - 退出代码 127,找不到捆绑命令
- bash - 使用 BASH 安装软件
- android - 修改 Android Paging Architecture 库中的 PagedList
- api - CKAN用户使用API登录
- java - 计算素数除以数字的次数
- javascript - XRegexP.matchRecursive - 添加回调功能以允许多个相同的实例
- javascript - 单击时显示缩略图的大图像,但包含多个容器
- java - MongoDB检查连接无异常
- c# - 麦克风输入的最大浮点数为1。如何将其变大并从1更改?统一 C#