addSubview之前的视图层次结构
视图> TableView
addSubview后的视图层次结构
视图> TableView && View > CustomView
调用addSubview后,CustomView显示在TableView的顶部,因为它的帧更小,并且在TableView之后添加。但是,我在CustomView中有按钮和文本字段,这些按钮和文本字段都不能工作。即使我点击CustomView,TableView的scrollView似乎也在捕捉tapGesture。如何使CustomView的按钮和文本字段接受触摸?
此外,CustomView的背景色是清晰的,在将customView添加到视图层次结构时,显示了下面的customView。我试着在故事板中设置背景颜色,并以编程的方式,但它仍然停留在清晰。我怎么才能解决这个问题?
class masterViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//blah blah
@IBOutlet weak var tableView: UITableView! // tableView is added in the storyboard
let newForm = NewFormView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
newForm.backgroundColor = UIColor.white
self.view.addSubview(newForm)
newForm.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
newForm.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
newForm.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
])
}newForm是一个UIView笔尖,有按钮和textFields添加到笔尖故事板中。
发布于 2019-05-25 20:11:57
您需要添加宽度和高度约束。
NSLayoutConstraint.activate([
newForm.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
newForm.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
newForm.widthAnchor.constraint(equalToConstant:300),
newForm.heightAnchor.constraint(equalToConstant:300)
])就像你做的那样
newForm.translatesAutoresizingMaskIntoConstraints = false您的帧变得无效,并且在接收触摸时不活跃。
https://stackoverflow.com/questions/56308279
复制相似问题