如何向我的 tabBarController 添加约束?
How do I add constraints to my tabBarController?
我添加了一个 tabBarController
并将其连接到 viewControllers
并给了 tabBarController
自己的 class
。它有效,但我想通过更改 constraints
来自定义它,使其不在底部。据我所知,无法在 auto layout
中添加 constraints
,因为它全部显示为灰色。我试过以编程方式添加它,但没有任何反应。
final class TabBarViewController: UITabBarController {
@IBOutlet var customTabBar: UITabBar!
override func viewDidLoad() {
self.selectedIndex = 2
let fontAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20.0)]
UITabBarItem.appearance().setTitleTextAttributes(fontAttributes, for: .normal)
//This doesn't work
customTabBar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
//Neither does this
self.tabBar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
//Or this
if let tabC = self.tabBarController {
tabC.tabBar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
}
}
那我该怎么做呢?我希望 tabBar
距底部约 50p。
你可以创建一个容器视图控制器来容纳你UITabBarController
。
然后,当您在容器视图上设置约束时,您可以将 'padding' 添加到底部,让它向上移动。下面是解释布局的两个屏幕截图。
我添加了一个 tabBarController
并将其连接到 viewControllers
并给了 tabBarController
自己的 class
。它有效,但我想通过更改 constraints
来自定义它,使其不在底部。据我所知,无法在 auto layout
中添加 constraints
,因为它全部显示为灰色。我试过以编程方式添加它,但没有任何反应。
final class TabBarViewController: UITabBarController {
@IBOutlet var customTabBar: UITabBar!
override func viewDidLoad() {
self.selectedIndex = 2
let fontAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 20.0)]
UITabBarItem.appearance().setTitleTextAttributes(fontAttributes, for: .normal)
//This doesn't work
customTabBar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
//Neither does this
self.tabBar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
//Or this
if let tabC = self.tabBarController {
tabC.tabBar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
}
}
那我该怎么做呢?我希望 tabBar
距底部约 50p。
你可以创建一个容器视图控制器来容纳你UITabBarController
。
然后,当您在容器视图上设置约束时,您可以将 'padding' 添加到底部,让它向上移动。下面是解释布局的两个屏幕截图。