Jak*_*kub 6 uitabbar ios autolayout swift safearealayoutguide
在将其标记为重复之前,请注意,我确实阅读了此内容,但这似乎不适用于UITabBar
双方UIViewController并UITabBar以编程方式创建。约束设置如下:
public override func viewDidLoad() {
super.viewDidLoad()
self.view.bringSubview(toFront: self.tabBar)
}
Run Code Online (Sandbox Code Playgroud)
并且self.tabBar:
lazy var tabBar: UITabBar = {
let tab = UITabBar()
self.view.addSubview(tab)
tab.translatesAutoresizingMaskIntoConstraints = false
tab.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
tab.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
tab.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true //This line will change in second part of this post.
return tab
}()
Run Code Online (Sandbox Code Playgroud)
这个节目UITabBar是这样的:
而且它太小了,因为它没有考虑安全区域。所以我确实改变了线路:
tab.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
Run Code Online (Sandbox Code Playgroud)
到:
tab.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor).isActive = true
Run Code Online (Sandbox Code Playgroud)
然后显示如下:
所以它也没有按预期显示。这里的目标是这样的:
如何制作约束以显示UITabBar这样的内容?
我在这里做到了:
let tabBar = UITabBar()
override func viewDidLoad() {
super.viewDidLoad()
addTabbar()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
addHeightConstraintToTabbar()
}
func addTabbar() -> Void {
self.view.addSubview(tabBar)
tabBar.translatesAutoresizingMaskIntoConstraints = false
tabBar.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
tabBar.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
tabBar.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
let item1 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.bookmarks, tag: 1)
let item2 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.contacts, tag: 2)
tabBar.items = [item1, item2]
self.view.bringSubview(toFront: tabBar)
}
func addHeightConstraintToTabbar() -> Void {
let heightConstant:CGFloat = self.view.safeAreaInsets.bottom + 49.0
tabBar.heightAnchor.constraint(equalToConstant: heightConstant).isActive = true
}
Run Code Online (Sandbox Code Playgroud)
结果:
可能不知道,这样做是否正确。你需要让它变得更好。
当您添加UItabbar到 UIViewController 并使用时,safeAreaLayoutGuide或者layoutMarginsGuide它将被添加到保存SafeAreaInsets底部有空间的视图控制器区域,您可以更改,但如果添加到视图,UIViewController它将坚持到边缘,没有任何空间,默认高度49
增加高度添加 heightAnchor约束
lazy var tabBar: UITabBar = {
let tab = UITabBar()
self.view.addSubview(tab)
tab.translatesAutoresizingMaskIntoConstraints = false
tab.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
tab.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
tab.heightAnchor.constraint(equalToConstant: 80).isActive = true
tab.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true //This line will change in second part of this post.
return tab
}()
Run Code Online (Sandbox Code Playgroud)
additionalSafeAreaInsets要使用安全区域指南,您可以向ViewController 的底部添加负值
self.additionalSafeAreaInsets = UIEdgeInsetsMake(0, 0, -39, 0)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7848 次 |
| 最近记录: |