5

当我在 SwiftUI 中使用工具栏组时,元素之间的空间太大。我把它HStack放在iOS 13中,没关系。但是当我在 iOS 14 中将它与工具栏一起放置时,出现了问题。我怎样才能解决这个问题?

var muteUser: some View {
        NavigationLink(destination: Text("dddd").environmentObject(CurrentUser)) {
            Image(systemName: "speaker.slash")
                .frame(width: 22, height: 22)
        }
}
var friendSetting: some View {
        NavigationLink(destination: FriendSettings().environmentObject(CurrentUser)) {
            Image(systemName: "gear")
                .frame(width: 22, height: 22)
        }
}

var body: some View {
    GeometryReader { geometry in
        if #available(iOS 14.0, *) {
            chatView
                .toolbar {
                    ToolbarItem(placement: .principal) {
                        centerNavBar()
                            .frame(maxWidth: geometry.size.width*0.75)
                    }
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        muteUser
                        friendSetting
                    }
                }
            
        } else {
            chatView
                .navigationBarItems(trailing:
                                        HStack(){
                                            centerNavBar()
                                            Spacer()
                                            rightNavBar
                                        }
                                        .frame(width: geometry.size.width*0.75)
                                    
                )
        }
    }
}

模拟器屏幕截图

4

2 回答 2

1

看起来这是里面两个按钮的标准外观ToolbarItemGroup

您可以使用 aHStack代替:

.toolbar {
    ToolbarItem(placement: .principal) {
        Image(systemName: "star.fill")
    }
    ToolbarItem(placement: .navigationBarTrailing) {
        HStack {
            NavigationLink(destination: Text("dddd").environmentObject(CurrentUser)) {
                Image(systemName: "speaker.slash")
                    .imageScale(.large)
            }
            NavigationLink(destination: FriendSettings().environmentObject(CurrentUser)) {
                Image(systemName: "gear")
                    .imageScale(.large)
            }
        }
    }
}
于 2020-12-23T20:26:36.917 回答
1

尝试省略.frame(width: 22, height: 22)工具栏上的图像项。我认为这导致 iOS 14 工具栏创建带有嵌入式视图而不是简单图像的 UIToolbarItems。

于 2020-12-24T19:46:25.843 回答