【问题标题】:How to prevent navigationBarTitle from taking up full width?如何防止 navigationBarTitle 占满全角?
【发布时间】:2020-11-22 10:01:53
【问题描述】:

我想将两个图标(刷新和设置)上移到与navigationBarTitle 相同的行:

这是我的看法:

struct Games: View {
    let diameter: CGFloat = 35.0
    
    var body: some View {
        NavigationView {
            VStack {
                // Create game
                HStack {
                    Image(systemName: "house.fill")
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 40, height: 40)
                        .foregroundColor(.blue)
                    Spacer()
                    Button(action: {
 
                    }){
                        Image(systemName: "arrow.clockwise")
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: 40, height: 40)
                            .foregroundColor(.gray)
                            .padding(.horizontal, 30)
                    }
                    NavigationLink(destination: Settings()){
                        Image("settings")
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: 40, height: 40)
                            .foregroundColor(.gray)
                    }
                }
                HStack{
                    Spacer()
                    Button(action: {  }){
                        Text("+ Create game")
                            .font(.custom("Seravek-Medium", size: 22))
                            .padding(15)
                            .background(
                                RoundedRectangle(cornerRadius: 5)
                                    .fill(Color.blue.opacity(0.1))
                            )
                    }
                }
                .frame(
                    maxWidth: .infinity,
                    maxHeight: 80,
                    alignment: .bottom
                )
            } // VStack
            .navigationTitle("Games").foregroundColor(.blue)
            .padding(25)
        } // NavigationView
    }
    
}

但似乎 navigationBarTitle 占据了整个宽度。

有什么办法可以解决这个问题吗?

【问题讨论】:

标签: ios swift swiftui


【解决方案1】:

我建议使用以下代码将这些图像添加到导航栏:

            .navigationBarItems(trailing:
                                    HStack {
                                        Button(action: {
                                            
                                        }){
                                            Image(systemName: "arrow.clockwise")
                                                .resizable()
                                                .aspectRatio(contentMode: .fill)
                                                .frame(width: 40, height: 40)
                                                .foregroundColor(.gray)
                                                .padding(.horizontal, 30)
                                        }
                                        NavigationLink(destination: Text("hello world")){
                                            Image("settings")
                                                .resizable()
                                                .aspectRatio(contentMode: .fill)
                                                .frame(width: 40, height: 40)
                                                .foregroundColor(.gray)
                                        }
                                    }
            )

作为替代方案,您可以使用上述工具栏,如下所示:

.toolbar {
                ToolbarItem(placement: .automatic) {
                    HStack {
                        Button(action: {

                        }){
                            Image(systemName: "arrow.clockwise")
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: 40, height: 40)
                                .foregroundColor(.gray)
                                .padding(.horizontal, 30)
                        }
                        NavigationLink(destination: Text("hello world")){
                            Image("settings")
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: 40, height: 40)
                                .foregroundColor(.gray)
                        }
                    }
                }
            }

【讨论】:

    猜你喜欢
    • 2021-07-01
    • 1970-01-01
    • 2011-06-29
    • 2019-03-11
    • 2022-08-19
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多