首页 > 解决方案 > UIView 上的按钮不响应,除非我指定正确的 CGRect

问题描述

我正在尝试实现一个简单的 UIView,它会在 Internet 连接丢失时显示在屏幕底部,并且会有一个关闭按钮。我的 UIView 类看起来像这样。

import UIKit

class ConnectionLostAlertUIView: UIView {

    @IBOutlet var view: UIView!
    var viewConstraints: [NSLayoutConstraint] = []

    override init(frame: CGRect) {
        super.init(frame: frame)
        Bundle.main.loadNibNamed("ConnectionLostAlertView", owner: self, options: nil)
        self.addSubview(self.view)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        Bundle.main.loadNibNamed("ConnectionLostAlertView", owner: self, options: nil)
        self.addSubview(self.view)
    }

    override func didMoveToSuperview() {

        view.translatesAutoresizingMaskIntoConstraints = false

        let bottomConstraint =     view.bottomAnchor.constraint(equalTo: self.superview!.bottomAnchor)
        let leftConstraint = view.leadingAnchor.constraint(equalTo: self.superview!.leadingAnchor)
        let rightConstraint = view.trailingAnchor.constraint(equalTo: self.superview!.trailingAnchor)

        viewConstraints = [bottomConstraint, leftConstraint, rightConstraint]
        NSLayoutConstraint.activate(viewConstraints)
    }

    @IBAction func dismissButton(_ sender: Any) {
        Logger.log(message: "Dismiss button was pressed", event: .d)
        self.isHidden = true
    }

我通过执行以下操作将此 UIView 添加到我的 UIViewController

var ConnectionLostAlert: ConnectionLostAlertUIView!
ConnectionLostAlert = ConnectionLostAlertUIView(frame: CGRect.zero)
self.view.addSubview(ConnectionLostAlert)

好吧,我可以使用 CGRect 的实际值而不是零,但问题是在我的 UIView 中添加约束后,我的 UIView 中的按钮停止响应。如果我实际上设置了一些 CGRect 坐标并且不在 UIView 中使用约束,它就可以正常工作。有没有更好的方法来做我想要实现的目标,仍然可以让我处理 UIView 中的按钮事件?

标签: iosswiftuiview

解决方案


问题是您正在从内部视图(即视图到超级视图)添加约束,跳过实际添加到超级视图的主视图(ConnectionLostAlertUIView)。

你应该做的是首先在视图和自我之间添加约束。然后当 self(ConnectionLostAlertUIView) 添加到超级视图时,然后添加带有超级视图的约束。

做这样的事情:

@IBOutlet var view: UIView!
var viewConstraints: [NSLayoutConstraint] = []

override init(frame: CGRect) {
    super.init(frame: frame)
    Bundle.main.loadNibNamed("ConnectionLostAlertView", owner: self, options: nil)
    self.addSubview(self.view)

    view.translatesAutoresizingMaskIntoConstraints = false

    let bottomConstraint =     view.bottomAnchor.constraint(equalTo: self.bottomAnchor)
    let leftConstraint = view.leadingAnchor.constraint(equalTo: self.leadingAnchor)
    let rightConstraint = view.trailingAnchor.constraint(equalTo: self.trailingAnchor)

    viewConstraints = [bottomConstraint, leftConstraint, rightConstraint]
    NSLayoutConstraint.activate(viewConstraints)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    Bundle.main.loadNibNamed("ConnectionLostAlertView", owner: self, options: nil)
    self.addSubview(self.view)
}

override func didMoveToSuperview() {

    self.translatesAutoresizingMaskIntoConstraints = false

    let bottomConstraint =     self.bottomAnchor.constraint(equalTo: self.superview!.bottomAnchor)
    let leftConstraint = self.leadingAnchor.constraint(equalTo: self.superview!.leadingAnchor)
    let rightConstraint = self.trailingAnchor.constraint(equalTo: self.superview!.trailingAnchor)

    viewConstraints = [bottomConstraint, leftConstraint, rightConstraint]
    NSLayoutConstraint.activate(viewConstraints)
}

@IBAction func dismissButton(_ sender: Any) {
    Logger.log(message: "Dismiss button was pressed", event: .d)
    self.isHidden = true
}

推荐阅读