ios - 具有自定义 UIView(包括按钮)的 UIViewController 无法识别水龙头

标签 ios swift uiview uiviewcontroller uibutton

当尝试以编程方式将包含 UIButton 的自定义 UIView (BaseHeader.swift) 加载到 UIViewController (ViewController.swift) 时,按钮上的点击被识别。

如果我提取自定义 UIView 中的代码并将其直接粘贴到 UIViewController 中,所有点击都会被识别并按预期工作。我错过了什么?

起初,我认为是 UIView 在实例化后没有定义框架大小的问题。我认为按钮可能没有“点击框”,尽管它是按预期显示的。在给 View 一个框架后,我仍然没有运气,在谷歌搜索了一段时间后尝试了各种其他方法。

The view loads but button taps are not recognized -- see image

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.title = "Authentication"
    }

    override func loadView() {
        super.loadView()

        let header = BaseHeader()
        header.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(header)

        NSLayoutConstraint.activate([
            header.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            header.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            header.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: -10),
        ])
    }
}

BaseHeader.swift

import UIKit

class BaseHeader: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    func setupView() {

        self.isUserInteractionEnabled = true

        let avenirFont = UIFont(name: "Avenir", size: 40)
        let lightAvenirTitle = avenirFont?.light

        let title = UILabel()
        title.text = "Title"
        title.font = lightAvenirTitle
        title.textColor = .black
        title.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(title)

        NSLayoutConstraint.activate([
            title.topAnchor.constraint(equalTo: self.topAnchor, constant: 20),
            title.centerXAnchor.constraint(equalTo: self.centerXAnchor)
        ])


        let profile = UIButton()
        let profileImage = UIImage(named: "person-icon")
        profile.setImage(profileImage, for: .normal)
        profile.setImage(UIImage(named: "think-icon"), for: .highlighted)
        profile.addTarget(self, action: #selector(profileTapped), for: .touchUpInside)
        profile.backgroundColor = .systemBlue
        profile.frame.size = CGSize(width: 30, height: 30)
        profile.isUserInteractionEnabled = true
        profile.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(profile)

        NSLayoutConstraint.activate([
            profile.centerYAnchor.constraint(equalTo: title.centerYAnchor),
            profile.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -20),
        ])
    }

    @objc func profileTapped(sender: UIButton) {
        print("Tapped")
    }
}

最佳答案

你忘记为 BaseHeader 设置高度

 NSLayoutConstraint.activate([
            header.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            header.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            header.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: -10),
header.heightAnchor.constraint(equalToConstant: 40) //add more
        ])

关于ios - 具有自定义 UIView(包括按钮)的 UIViewController 无法识别水龙头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59014062/

相关文章:

ios - UICollectionView 项目按其部分位置偏移

swift - 数组上的条件绑定(bind)

ios - 这会导致崩溃吗?或者我们也可以在 Swift 中向 nil 发送消息......?

xcode - 错误无法将 UIView 类型的值转换为 SKview

ios - 仅横向 iOS 应用程序无法正确显示(与 shouldAutorotateToInterfaceOrientation 无关)

iOS 在 swift 主 StoryBoard 之前创建一个欢迎页面

ios - 使用 Objective-C 的 AES Java 加密 16 字节 key 解密

iOS/iPhone : in-app purchase sandbox broken while app in "rejected" state?

ios - UIImageView.layer.cornerRadius 在不同像素密度上创建圆形图像的问题 ios

ios - 设置 UIButton 的 titleLabel 的字体会将 textColor 更改为默认字体?