ios - 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 中的按钮事件?
解决方案
问题是您正在从内部视图(即视图到超级视图)添加约束,跳过实际添加到超级视图的主视图(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
}
推荐阅读
- wordpress - Wordpress - 来自前端的 CRUD 操作
- json - 从 laravel 中的 json 数据中获取特定值
- reactjs - react-final-form input.onChange(null) 不会立即更新状态
- javascript - vue table 显示数据 v-for v-if
- git - 一次提交更新所有分支
- vim - 执行 :Ex 时如何阻止 Vim 水平拆分窗口
- r - R:R是否自然保留行的顺序
- python - 如何在python中使用模拟json数据库文件
- python - 似乎无法从下拉菜单中选择 - Python Selenium
- ruby-on-rails - 如何在 ruby 代码中使用 Stripe 令牌通过 Stripe::Account.create() 创建帐户?