【问题标题】:UIViewController with custom UIView (that includes a button) doesn't recognize taps带有自定义 UIView(包括按钮)的 UIViewController 无法识别水龙头
【发布时间】:2020-03-19 16:42:04
【问题描述】:

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

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

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

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")
    }
}

【问题讨论】:

    标签: ios swift uiview uiviewcontroller uibutton


    【解决方案1】:

    可能是您为自定义视图调用了错误的初始化程序,并且您的 setupView() 函数没有被调用。它看起来像在您正在调用的视图控制器中

    let header = BaseHeader()
    

    要初始化视图,不过是这个初始化:

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

    这会调用您的 setupView() 函数并设置水龙头。

    【讨论】:

      【解决方案2】:

      您忘记为 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
              ])
      
      

      【讨论】:

      • 谢谢这是修复。您能否详细说明在视图本身中为自定义 UIView 设置约束与在初始化它的视图控制器中设置约束?
      • 自动布局非常棒,我想你应该看看这个帖子theswiftdev.com/2018/06/14/…raywenderlich.com/…
      • 很棒的阅读,我还没有遇到过那个。我很感激,干杯队友@Giang
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多