我正在尝试在项目中的所有视图控制器中使用错误消息视图“当网络连接不良时可以使用Facebook应用程序”。 所以我创建了一个带有标签(错误消息)和关闭按钮的.xib文件。
以下是管理UIView的课程:
class ErrorMessageView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet var messageLabel: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
private func commonInit() {
NSBundle.mainBundle().loadNibNamed("ErrorMessageView", owner: self, options: nil)
guard let content = contentView else { return }
content.frame = self.frame
self.addSubview(content)
}
@IBAction func cancelAction(sender: UIButton) {
self.hidden = true
}
class func setupErrorView(tableView: UITableView) -> ErrorMessageView {
// Initialize an Error Message View
let errorMessageView = ErrorMessageView()
tableView.addSubview(errorMessageView)
// Setup its constraints
errorMessageView.translatesAutoresizingMaskIntoConstraints = false
let topConstraint = NSLayoutConstraint(item: tableView, attribute: .Top, relatedBy: .Equal, toItem: errorMessageView, attribute: .Top, multiplier: 1.0, constant: 0.0)
tableView.addConstraint(topConstraint)
let widthConstraint = NSLayoutConstraint(item: tableView, attribute: .Width, relatedBy: .Equal, toItem: errorMessageView, attribute: .Width, multiplier: 1.0, constant: 0.0)
tableView.addConstraint(widthConstraint)
let heightConstraint = NSLayoutConstraint(item: errorMessageView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 44.0)
errorMessageView.addConstraint(heightConstraint)
errorMessageView.hidden = true
return errorMessageView
}
}
在每个视图控制器中调用最后一个类函数,其中可能存在网络连接错误,如果错误发生,我只显示ErrorMessageView
。
它显示了iPhone和iPad上所有方向的完美位置 - 但在iPhone上我得到了一个复杂的约束'错误(尽管在模拟器中视图显得正常!“。这是我得到的错误!
(
"<NSAutoresizingMaskLayoutConstraint:0x7fccda9bd6e0 h=-&- v=-&- UIView:0x7fccda9b6da0.width == Arti.ErrorMessageView:0x7fccda9b4b70.width>",
"<NSLayoutConstraint:0x7fccda9b7590 UILabel:0x7fccda9b6f10'TEST ERROR MESSAGE VIEW'.leading == UIView:0x7fccda9b6da0.leadingMargin>",
"<NSLayoutConstraint:0x7fccda9b7680 H:[UILabel:0x7fccda9b6f10'TEST ERROR MESSAGE VIEW']-(NSSpace(8))-[UIButton:0x7fccda9b67a0]>",
"<NSLayoutConstraint:0x7fccda9b76d0 H:[UIButton:0x7fccda9b67a0]-(NSSpace(20))-| (Names: '|':UIView:0x7fccda9b6da0 )>",
"<NSLayoutConstraint:0x7fccda9aaca0 UITableView:0x7fccd890e400.leading == UIView:0x7fccda9aa180.leading>",
"<NSLayoutConstraint:0x7fccda9aad40 UITableView:0x7fccd890e400.trailing == UIView:0x7fccda9aa010.trailing>",
"<NSLayoutConstraint:0x7fccda9aade0 H:|-(0)-[UITableView:0x7fccd890e400] (Names: '|':UIView:0x7fccda9a9ea0 )>",
"<NSLayoutConstraint:0x7fccda9aae30 UIView:0x7fccda9aa180.width == UIView:0x7fccda9aa010.width>",
"<NSLayoutConstraint:0x7fccda9aae80 H:[UIView:0x7fccda9aa180]-(NSSpace(8))-[UIView:0x7fccda9a60e0]>",
"<NSLayoutConstraint:0x7fccda9aaf20 UIView:0x7fccda9a60e0.centerX == UIView:0x7fccda9a9ea0.centerX>",
"<NSLayoutConstraint:0x7fccda9ab150 H:[UIView:0x7fccda9a60e0]-(NSSpace(8))-[UIView:0x7fccda9aa010]>",
"<NSLayoutConstraint:0x7fccda9ba8c0 UITableView:0x7fccd890e400.width == Arti.ErrorMessageView:0x7fccda9b4b70.width>",
"<NSLayoutConstraint:0x7fccda9bf9a0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fccda9a9ea0(0)]>"
)
答案 0 :(得分:0)
从您的描述和日志中,我会说您的视图比您正在使用的UITableView更广泛。这可能就是为什么你只能在较窄的iPhone屏幕上得到错误。
要测试是否是这种情况,请将您的xib宽度更改为您可以在应用中显示此视图的最小尺寸,并检查您的约束是否正常。
答案 1 :(得分:0)
我建议你在.xib中添加约束,这样你就可以看到哪个Constraint是错误的。 Xcode会用红点或黄点标记它们。
调整.xib文件中的视图大小,使其在初始化错误消息视图时等于帧大小。
删除所有约束,然后在.xib中再次添加它们。