【发布时间】:2020-10-16 09:32:58
【问题描述】:
我正在使用 Google Maps API,并且我创建了一个 UIButton 来放置在地图视图的顶部。在那个 UIButton 里面,我插入了一个 UIImageView。出于某种原因,在我向 UIButton 添加目标操作后,没有调用选择器方法。以下是构建应用程序时元素外观的示例:
这是我的选择器方法:
@objc func goToListView() {
print("Go to list view!")
}
这是我的 UIButton 代码:
func configureReturnToListViewItem() {
view.addSubview(returnToListViewItem)
returnToListViewItem.backgroundColor = .white
returnToListViewItem.layer.cornerRadius = 25
returnToListViewItem.clipsToBounds = true
returnToListViewItem.isUserInteractionEnabled = true
returnToListViewItem.translatesAutoresizingMaskIntoConstraints = false
returnToListViewItem.heightAnchor.constraint(equalToConstant: 50).isActive = true
returnToListViewItem.widthAnchor.constraint(equalToConstant: 50).isActive = true
returnToListViewItem.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 25).isActive = true
returnToListViewItem.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
returnToListViewItem.addTarget(self, action: #selector(goToListView), for: .touchUpInside)
}
这是 UIButton 内的图像代码,以防万一:
func configureReturnToListViewImage() {
returnToListViewItem.addSubview(returnToListViewImage)
let image = UIImage(named: "list")
returnToListViewImage.image = image
returnToListViewImage.contentMode = .scaleAspectFill
returnToListViewImage.clipsToBounds = true
returnToListViewImage.layer.masksToBounds = true
returnToListViewImage.translatesAutoresizingMaskIntoConstraints = false
returnToListViewImage.heightAnchor.constraint(equalToConstant: 20).isActive = true
returnToListViewImage.widthAnchor.constraint(equalToConstant: 20).isActive = true
returnToListViewImage.centerXAnchor.constraint(equalTo: returnToListViewItem.centerXAnchor).isActive = true
returnToListViewImage.centerYAnchor.constraint(equalTo: returnToListViewItem.centerYAnchor).isActive = true
}
我尝试过点击手势,以及将视图控制器中的所有元素的 isUserInteractionEnabled 设置为 true。我希望得到一些指导!
编辑:我的问题是由于早期配置导致导航栏不可见。在大多数情况下,其他答案是正确的 - 因此,如果它们适用于您的情况,请也使用这些答案。
【问题讨论】:
标签: ios swift uibutton autolayout uiimageview