创建包含3个项目的导航栏

时间:2020-08-26 13:18:26

标签: swift swiftui

我正在尝试创建类似这样的东西:

enter image description here

一个包含3个项目的导航栏,是否可以使用class allProduct { vector <std::shared_ptr<Product>>productList; int numOfProducts; } allProduct::allProduct(const allProduct& other) { vector <std::shared_ptr<Product>>NewproductList; typename vector <std::shared_ptr<Product>>::iterator it = other.productList.begin(); for (; it != other.productList.end(); it++) { NewproductList.begin() = other.productList.begin(); } } allProduct& allProduct::operator=(const allProduct& other) { numOfProducts = other.numOfProducts; typename vector <std::shared_ptr<Product>>::iterator it = other.productList.begin(); for (;it !=(other.productList.end());it++) { addProduct(other.productList.begin()); other.productList.begin()++; } return *this; } void allProduct::addProduct(Product* pro) { numOfProducts++; this->productList.push_back(pro); } 来做到这一点?

我当前的计划是使用以下方法隐藏导航栏:

navigationBarItems

,然后使用.navigationBarTitle("") .navigationBarHidden(true) 创建3个按钮。我遇到的问题是因为我要隐藏导航栏,单击其中一个按钮会将其带到另一个视图,这也将隐藏导航栏(那不是我正在寻找的东西)

我尝试过:

HStack

但这会在右侧创建两个相邻的项目。我尝试将.navigationBarItems(trailing: HStack { Button("About") { print("About tapped!") } Button("Help") { print("Help tapped!") } } ) 放在上面的Spacer()中,但这不起作用。

我更喜欢使用HStack,但似乎找不到找到项目居中的方法?

1 个答案:

答案 0 :(得分:3)

具有3个项目的导航栏,是否可以使用navigationBarItems做到这一点?

不。此外,自SwiftUI 2.0起,navigationBarItems修饰符已弃用

SwiftUI 2.0

可以使用toolbar修饰符轻松完成此操作,就像将其附加到NavigationView内的任何视图一样

使用Xcode 12 / iOS 14准备并测试了演示:

demo

.toolbar {
    ToolbarItem(placement: .primaryAction) {
        Button(action: {}) { Image(systemName: "gear") }
    }
    ToolbarItem(placement: .principal) {
        Button(action: {}) { Image(systemName: "car") }
    }
    ToolbarItem(placement: .navigation) {
        Button(action: {}) { Image(systemName: "chevron.left") }
    }