【发布时间】:2017-02-15 21:46:32
【问题描述】:
我有一个UITableViewController,我想在它上面放置一个 UIView(不是覆盖/覆盖,就在上面),同时保留导航栏。
这是我正在努力实现的一个很好的例子,除了我说我仍然想要导航栏:
我在 Swift (3) 中执行此操作,并且全部以编程方式执行(无情节提要)。
这是我目前在我的UITableViewController 中所拥有的,在以编程方式执行约束时我被卡住了,我不确定我需要什么:
// Create the view (add subview in viewDidLoad)
let topView: UIView = {
let tv = UIView()
tv.backgroundColor = UIColor.darkGray
tv.translatesAutoresizingMaskIntoConstraints = false
tv.layer.masksToBounds = true
return tv
}()
// Constraints (call in viewDidLoad)
func setupTopView() {
//x, y, width, height constraints
topView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
topView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
topView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
topView.heightAnchor.constraint(equalToConstant: 100)
}
有人知道怎么做吗?
编辑:谢谢@Sneak,这就是我最终得到的结果:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerLabel = UILabel()
let logoView = UIImageView()
headerLabel.frame = CGRect(x: 0, y: 0, width: 200, height: 21)
headerLabel.text = self.navigationItem.title
headerLabel.textColor = UIColor.white
headerLabel.backgroundColor = UIColor.clear
logoView.frame = CGRect(x: 0, y: 0, width: 90, height: 90)
let logo: UIImage = UIImage(named: self.navigationItem.title!)!
logoView.image = logo
view.addSubview(headerLabel)
view.addSubview(logoView)
return topView
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 175
}
【问题讨论】:
-
您可以使用常规的`viewController`和
tableView,或者将您的视图作为标题添加到当前tableView
标签: ios swift uitableview uiview