How to Position a Navigation Bar Title Above a Picker in SwiftUI?

I am developing a SwiftUI interface that aims to replicate a particular Apple-style navigation bar, where the title is positioned directly above a segmented picker, and both elements share a translucent background (using .thinMaterial). This design allows the content of the view below to be visible through the navigation bar as it scrolls.

However, I’m encountering an issue where using the .principal toolbar placement removes the inline title and positions the picker between the leading and trailing toolbar items, rather than below all these elements.

As you can see, Apple do it perfectly here:

import SwiftUI

struct TestView: View {
    @State private var selectedView = 0
    let pickerOptions = ["First View", "Second View"]

    var body: some View {
        NavigationView {
            VStack(spacing: 0) {
                Group {
                    if selectedView == 0 {
                        FirstView()
                    } else {
                        SecondView()
                    }
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
            }
            .navigationBarTitle("Title", displayMode: .inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button(action: {}) {
                        Image(systemName: "ellipsis.circle")
                    }
                }
                ToolbarItem(placement: .principal) {
                    Picker("", selection: $selectedView) {
                        ForEach(pickerOptions.indices, id: .self) { index in
                            Text(self.pickerOptions[index]).tag(index)
                        }
                    }
                    .pickerStyle(SegmentedPickerStyle())
                }
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: {}) {
                        Image(systemName: "pencil.tip.crop.circle")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(height: 35)
                        Image(systemName: "person.crop.circle")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(height: 35)
                    }
                    .foregroundStyle(.white)
                }
            }
            .background(.thinMaterial)
        }
    }
}

My goal is to adjust the placement so that the picker is situated directly below the title within the navigation bar, all while maintaining a clear visual pathway to the content behind through the translucent navigation bar equipment. How can I achieve this layout in SwiftUI?

I attempted to integrate the picker within the navigation bar by placing it in the .principal section of the toolbar. My expectation was that this would allow the picker to appear just below the navigation title, still within the navigation bar area, to create a unified appearance as seen in Apple’s interfaces.

What I Was Expecting:

I expected the picker to align directly below the navigation title, forming a seamless continuation of the navigation bar. The goal was for the picker and the title to both have a thin, translucent material design, allowing the content scrolling behind them to be visible, enhancing the user interface’s aesthetic and functionality.

What Happened Instead:

Instead of the picker aligning below the title, using .principal placement led to the picker being positioned between the leading and trailing toolbar items. This not only disrupted the expected layout but also removed the inline display of the navigation title, contrary to my goal of a seamless integration similar to Apple’s design.

New contributor

cxrterz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

In @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)

You can use safeAreaInset

        .safeAreaInset(edge: .top, content: {
            Picker("", selection: $selectedView) {
                ForEach(pickerOptions.indices, id: .self) { index in
                    Text(self.pickerOptions[index]).tag(index)
                }
            }
            .pickerStyle(SegmentedPickerStyle())
        })

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật