Swift - 以编程方式创建的按钮和标签相互链接
Posted
技术标签:
【中文标题】Swift - 以编程方式创建的按钮和标签相互链接【英文标题】:Swift - Linking programmatically created buttons and labels to each other 【发布时间】:2018-11-27 22:38:53 【问题描述】:我正在以编程方式创建所有内容,但在链接多个按钮以对选定的自定义操作做出反应时遇到问题。
蓝色按钮是以编程方式创建的,我使用标签来跟踪按下了哪个按钮。选择蓝色按钮时,弹出动作菜单,可以通过按下添加图标将动作链接到按钮。
您可以通过单击“选择”来选择所需的操作,然后从“o”按钮中按下并拖动以创建连接器。
我将选定的操作以及与之连接的按钮存储在字典中
问题是当以编程方式创建它们时,如何将同一行中的“o”按钮、“选择”按钮和“名称”标签相互链接?我没有使用 tableview 来创建操作。那会更容易使用吗?
这将创建操作行
// MARK: - ACTION Input
func createAction()
let actionTabContainer = UIView()
actionTabContainer.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
actionTabContainer.translatesAutoresizingMaskIntoConstraints = false
actionTabContainer.backgroundColor = UIColor.darkGray
actionTabContainer.layer.borderWidth = 2
actionTabContainer.layer.borderColor = UIColor(red: 29/255.0, green: 30/255.0, blue: 33/255.0, alpha: 1.0).cgColor
actionScrollViewContainer.addSubview(actionTabContainer)
actionTabContainer.widthAnchor.constraint(equalToConstant: actionScrollViewContainer.frame.width).isActive = true
actionTabContainer.heightAnchor.constraint(equalToConstant: 50).isActive = true
actionTabContainer.leftAnchor.constraint(equalTo: actionScrollViewContainer.leftAnchor, constant: 10).isActive = true
actionTabContainer.topAnchor.constraint(equalTo: actionScrollViewContainer.topAnchor, constant: 2 + constantAdd).isActive = true
constantAdd = constantAdd + 50
let connectorBtn = UIButton()
connectorBtn.createRectangleButton(buttonPositionX: 0, buttonPositionY: 0, buttonWidth: 0, buttonHeight: 0, buttonTitle: "O", buttonTag: 400)
connectorBtn.translatesAutoresizingMaskIntoConstraints = false
connectorBtn.backgroundColor = UIColor.gray
actionTabContainer.addSubview(connectorBtn)
connectorBtn.widthAnchor.constraint(equalToConstant: 30).isActive = true
connectorBtn.heightAnchor.constraint(equalToConstant: 30).isActive = true
connectorBtn.leftAnchor.constraint(equalTo: actionTabContainer.leftAnchor, constant: 10).isActive = true
connectorBtn.centerYAnchor.constraint(equalTo: actionTabContainer.centerYAnchor, constant: 0).isActive = true
connectorBtn.addTarget(self, action: #selector(addConnector(sender:)), for: .touchUpInside)
addPanReconiser(view: connectorBtn)
let chooseActionButton = UIButton()
chooseActionButton.createRectangleButton(buttonPositionX: 0, buttonPositionY: 0, buttonWidth: 0, buttonHeight: 0, buttonTitle: "select", buttonTag: 700)
chooseActionButton.translatesAutoresizingMaskIntoConstraints = false
chooseActionButton.backgroundColor = UIColor.gray
chooseActionButton.layer.cornerRadius = 0
actionTabContainer.addSubview(chooseActionButton)
chooseActionButton.widthAnchor.constraint(equalToConstant: 110).isActive = true
chooseActionButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
chooseActionButton.leftAnchor.constraint(equalTo: connectorBtn.rightAnchor, constant: 10).isActive = true
chooseActionButton.centerYAnchor.constraint(equalTo: connectorBtn.centerYAnchor, constant: 0).isActive = true
chooseActionButton.addTarget(self, action: #selector(addConnector(sender:)), for: .touchUpInside)
let actionMarkerConnectedLabel = UILabel()
actionMarkerConnectedLabel.createLabel(labelPositionX: 0, labelPositionY: 0, labelWidth: 0, labelHeight: 0, labelTitle: "name")
actionMarkerConnectedLabel.backgroundColor = UIColor.gray
actionMarkerConnectedLabel.translatesAutoresizingMaskIntoConstraints = false
actionMarkerConnectedLabel.textAlignment = .center
connectorBtn.addSubview(actionMarkerConnectedLabel)
actionMarkerConnectedLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
actionMarkerConnectedLabel.heightAnchor.constraint(equalToConstant: 32).isActive = true
actionMarkerConnectedLabel.leftAnchor.constraint(equalTo: chooseActionButton.rightAnchor, constant: 10).isActive = true
actionMarkerConnectedLabel.centerYAnchor.constraint(equalTo: connectorBtn.centerYAnchor, constant: 0).isActive = true
我有自己的扩展程序来创建可能看起来令人困惑的矩形和其他形状。
感谢您的任何建议
【问题讨论】:
【参考方案1】:我认为如果使用表格视图并通过 indexPath 控制它们会更好。如果没有,您可以创建一个变量来保持“计数”,然后在创建新时:
count += 1
button1.tag = count
button2.tag = count
【讨论】:
谢谢Lê Minh,我会试一试 嘿 Lê Minh,我使用了 tableview,但标签与我以编程方式创建的其他按钮重叠。你还能想到其他方法吗?谢谢 您是否通过 indexPath 设置了标签,例如:= indexpath.row 或者如果您使用 section 那么将是 indexPath.section 我将标签设置为默认值 700,它会停止重叠以上是关于Swift - 以编程方式创建的按钮和标签相互链接的主要内容,如果未能解决你的问题,请参考以下文章
Swift 3:不能以编程方式在 Collectionview 标题上正确创建标签?