提问人:Austie Chou 提问时间:8/25/2023 最后编辑:Austie Chou 更新时间:8/28/2023 访问量:61
iOS AutoLayout 问题(Tabman 和 SnapKit):无法将一个容器的底部与安全区域的顶部对齐
iOS AutoLayout issue(Tabman & SnapKit): Having trouble aligning the bottom of one container with the top of safe area
问:
我在自动布局方面遇到了问题(我正在使用 SnapKit)。
我使用 Tabman(Tabman 文档在这里)创建我的主视图。
我使用 Tabman 的 API 为选项卡创建自定义布局。
这是我的代码:
import UIKit
import Tabman
import Pageboy
import SnapKit
class HomeViewController: TabmanViewController {
enum Tab: String, CaseIterable {
case follow
case explore
}
private let tabItems = Tab.allCases.map({ BarItem(for: $0) })
private lazy var viewControllers = tabItems.compactMap({ $0.makeViewController() })
let tabBar = HomeViewTabBar.make()
let barContainer = UIView()
var searchBar = HomeSearchBarView()
override func viewDidLoad() {
super.viewDidLoad()
// PageView dataSource and delegate
self.dataSource = self
view.backgroundColor = .systemBackground
setupSearchBarLayout()
setupBarContainerLayout()
setupTabBar()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tabBar.layer.masksToBounds = false
}
private func setupSearchBarLayout(){
view.addSubview(searchBar)
searchBar.translatesAutoresizingMaskIntoConstraints = false
searchBar.snp.makeConstraints{ make -> Void in
make.top.equalTo(self.view).offset(self.view.statusBarHeight)
make.left.equalTo(self.view.safeAreaLayoutGuide)
make.right.equalTo(self.view.safeAreaLayoutGuide)
make.bottom.equalTo(self.searchBar.snp.top).offset(self.view.statusBarHeight)
}
}
private func setupBarContainerLayout(){
view.addSubview(barContainer)
barContainer.snp.makeConstraints{ make -> Void in
make.top.equalTo(self.searchBar.snp.bottom)
make.left.equalTo(self.view.safeAreaLayoutGuide)
make.right.equalTo(self.view.safeAreaLayoutGuide)
make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.top)
}
}
private func setupTabBar(){
addBar(tabBar, dataSource: self, at: .custom(view: barContainer, layout: { bar in
bar.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
bar.topAnchor.constraint(equalTo: self.barContainer.topAnchor),
bar.centerXAnchor.constraint(equalTo: self.barContainer.centerXAnchor)
])
}))
}
}
因此,在上面的代码中,我为选项卡栏创建了一个容器以使用 Tabman 自定义布局
我试图下车,将一个容器的底部与下一个容器的顶部对齐:但这只是让我的自动布局冲突。删除它后,一切都很好。make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.top)
anyuone可以教我吗,谢谢!
您能为我提供一些关于自动布局的基本解释吗?
答: 暂无答案
评论
HomeSearchBarView
HomeViewTabBar
barContainer
bottom
safeAreaLayoutGuide.snp.top