以编程方式创建的UIViewController中带有安全区域内的UITabBar

10
请注意,在您将其标记为重复之前,请注意,我已经阅读了此文,并且似乎不适用于UITabBarUIViewControllerUITabBar都是以编程方式创建的。 约束条件设置如下:
public override func viewDidLoad() {
    super.viewDidLoad()
    self.view.bringSubview(toFront: self.tabBar)
}

关于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
}()

这个展示像这样的UITabBar

enter image description here

由于它没有考虑安全区域,所以太小了。因此,我更改了以下行:

tab.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

to:

tab.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor).isActive = true

然后它会被显示为这样:
enter image description here
所以它也没有如预期那样显示。这里的目标是像这样显示:
enter image description here
如何设置约束来显示UITabBar

你为什么要自己添加选项卡栏?最好使用UITabBarController。 - Scriptable
4
我不需要 UITabBarController。添加 UITabBar 有什么问题吗? - Jakub
我熟悉 UITabBarControllerUITabBar,但我并不需要大多数 UITabBarController 的功能。UITabBar 没有任何问题。正如苹果文档所述:通常,你会使用 UITabBarController 对象来配合使用选项卡栏,但你也可以将其作为独立控件在你的应用程序中使用。 - Jakub
我知道你可以单独使用它,我只是问了一个问题,以便澄清。 - Scriptable
你只需要以编程方式设置吗?我是使用界面构建器设置的。 - Krunal
显示剩余3条评论
3个回答

10

我做到了:

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
}

结果: enter image description here 可能不知道这样做是否正确。您需要将其改进。

@PranavKasetti 你可以在答案中看到两种设备(iPhone-X和不是iPhoneX的设备(iPhone SE))的结果。请详细解释你的问题。欢迎你在这里提供你的意见来提高这个答案的质量。 - Krunal
如何将自定义的视图控制器添加到选项卡项目数组中 - PvDev

3
当您将UITabBar添加到UIViewController并使用safeAreaLayoutGuidelayoutMarginsGuide时,它将被添加到该视图控制器的安全区域中,该安全区域具有底部可以更改的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
    }()

为了使用安全区域指南,您可以将负值添加到ViewController的additionalSafeAreaInsets底部。

self.additionalSafeAreaInsets = UIEdgeInsetsMake(0, 0, -39, 0)

1
底部锚点需要连接到视图的 safeAreaLayoutGuide
tabbar.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    tabbar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    tabbar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
    tabbar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])

无法与控制器一起使用:崩溃“无法为由控制器管理的UITabBar选择布局方法” - djdance
@djdance 原始答案是针对具有添加为子视图的 UITabBar 的自定义 UIViewController。如果您想要子类化 UITabBarController,那么您将需要隐藏默认的 tabbar 并添加您自己的以避免出现此错误。 - Divyesh Makwana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接