ios - 如何在视图中的特定点添加子视图
问题描述
我正在向 tableView.tableHeaderView 添加一个 imageView 和 UISearchController.searchBar。但没有找到正确的方法在图像之后显示 searchBar。
let view = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 333))
let image = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 269))
image.image = #imageLiteral(resourceName: "map")
view.addSubview(image)
view.insertSubview(searchController.searchBar, at: 1)//insertSubview(searchController.searchBar, belowSubview: image)
self.tableView.tableHeaderView = view
解决方案
如果您想在视图底部添加搜索栏 您可以以编程方式添加搜索栏并将其固定到视图底部
view.addSubview(searchBar)
searchBar.bottomAnchor.constraint(equalTo: view.trailingAnchor).isActive =true
searchBar.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
searchBar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
searchBar.heightAnchor.constraint(equalToConstant: 50).isActive = true
最简单的方法
推荐阅读
- powershell - 如何让 Powershell 的 Import-Csv 正确导入空格
- class - SwiftUi 初始化一个类中的主结构
- java - 具有复杂 HashMap 结构的 GSON 到 Json
- java - 如何使用@Async在spring boot中为一种方法创建多个线程池
- amazon-web-services - 如何在`serverless.yml`的`Resources`中使用`If`条件?
- google-sheets - Vlookup 从一列将值放在多列中
- html - 如何使用 AWS CloudFront 和 EC2 在 php 页面之间传递 multipart/form-data
- python - 我如何使用 binance api 从符号中查看即时交易
- r - 如何将输入和输出表转换为矩阵?
- java - 使用嵌套调用编写 Spring Boot 控制器