向 UITableView 添加页脚
Adding footer to UITableView
因此,我制作了一个带有自定义单元格(.xib 文件)的 UITableView,效果很好。问题是我现在想添加一个页脚,它需要一个 UIImageView 和一个 UILabel。
这就是我目前正在做的事情:(当然我是在viewDidLoad()
里面执行这个函数)
private func setupTableViewFooter() {
/// Insight Logo
let myAppLogo = UIImageView()
myAppLogo.backgroundColor = .clear
myAppLogo.widthAnchor.constraint(equalToConstant: 110.0).isActive = true
myAppLogo.heightAnchor.constraint(equalToConstant: 60.0).isActive = true
myAppLogo.contentMode = .scaleAspectFit
myAppLogo.image = UIImage(named: "myAppLogo")
/// Description Label
let label = UILabel()
label.backgroundColor = .clear
label.widthAnchor.constraint(equalToConstant: self.view.frame.size.width - 60).isActive = true
label.heightAnchor.constraint(equalToConstant: 30.0).isActive = true
label.textAlignment = .left
label.textColor = K.Colors.black
label.font = UIFont(name: "Roboto-Medium", size: 13)
label.text = "Hello world!"
/// Stack View
let footer = UIStackView()
footer.axis = .vertical
footer.distribution = .fill
footer.alignment = .leading
footer.spacing = 00.0
footer.backgroundColor = .lightGray
footer.addArrangedSubview(myAppLogo)
footer.addArrangedSubview(label)
footer.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
footer.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30),
footer.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -30),
footer.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -60),
footer.heightAnchor.constraint(equalToConstant: 90)
])
self.tableView.tableFooterView = footer
}
我收到此错误:
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutXAxisAnchor:0x282fa0640 "UIStackView:0x108022090.left"> and <NSLayoutXAxisAnchor:0x282f56400 "UIView:0x108020ec0.left"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
terminating with uncaught exception of type NSException
我不习惯以编程方式设置约束,所以我很难解决这个问题。请帮忙!
首先将页脚添加到 uitableView,然后添加约束
100% 成功测试
self.tableView.tableFooterView = footer
NSLayoutConstraint.activate([ footer.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30), footer.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -30), footer.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -60), footer.heightAnchor.constraint(equalToConstant: 90) ])
因此,我制作了一个带有自定义单元格(.xib 文件)的 UITableView,效果很好。问题是我现在想添加一个页脚,它需要一个 UIImageView 和一个 UILabel。
这就是我目前正在做的事情:(当然我是在viewDidLoad()
里面执行这个函数)
private func setupTableViewFooter() {
/// Insight Logo
let myAppLogo = UIImageView()
myAppLogo.backgroundColor = .clear
myAppLogo.widthAnchor.constraint(equalToConstant: 110.0).isActive = true
myAppLogo.heightAnchor.constraint(equalToConstant: 60.0).isActive = true
myAppLogo.contentMode = .scaleAspectFit
myAppLogo.image = UIImage(named: "myAppLogo")
/// Description Label
let label = UILabel()
label.backgroundColor = .clear
label.widthAnchor.constraint(equalToConstant: self.view.frame.size.width - 60).isActive = true
label.heightAnchor.constraint(equalToConstant: 30.0).isActive = true
label.textAlignment = .left
label.textColor = K.Colors.black
label.font = UIFont(name: "Roboto-Medium", size: 13)
label.text = "Hello world!"
/// Stack View
let footer = UIStackView()
footer.axis = .vertical
footer.distribution = .fill
footer.alignment = .leading
footer.spacing = 00.0
footer.backgroundColor = .lightGray
footer.addArrangedSubview(myAppLogo)
footer.addArrangedSubview(label)
footer.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
footer.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30),
footer.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -30),
footer.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -60),
footer.heightAnchor.constraint(equalToConstant: 90)
])
self.tableView.tableFooterView = footer
}
我收到此错误:
libc++abi.dylib: terminating with uncaught exception of type NSException *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutXAxisAnchor:0x282fa0640 "UIStackView:0x108022090.left"> and <NSLayoutXAxisAnchor:0x282f56400 "UIView:0x108020ec0.left"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.' terminating with uncaught exception of type NSException
我不习惯以编程方式设置约束,所以我很难解决这个问题。请帮忙!
首先将页脚添加到 uitableView,然后添加约束
100% 成功测试
self.tableView.tableFooterView = footer
NSLayoutConstraint.activate([ footer.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 30), footer.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -30), footer.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -60), footer.heightAnchor.constraint(equalToConstant: 90) ])