嵌入到NavigationView SwiftUI中时,使列表节在SwiftUI中不可折叠

问题描述

当我将分组为List的{​​{1}}嵌入到Section中时,节标题会变得可折叠。我想让它们不可折叠,就像NavigationView未嵌入List中一样。

我当前的代码(带有NavigationView):

NavigationView

list with collapsible sections

解决方法

这是应用的默认样式,您可以将其显式设置为List,如下所示(已通过Xcode 12 / iOS 14测试)

demo

    List {
        ForEach(groups,id: \.self.name) { group in
            Section(header: Text(group.name)) {
                ForEach(group.items,id:\.self) { item in
                    Text(item)
                }
            }
        }
    }.listStyle(InsetGroupedListStyle()) // or GroupedListStyle
,

只需在 listStyle 修饰符中使用 SidebarListStyle

HRESULT hr = S_OK;

// Register the window class.
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH));
wc.lpszClassName = L"DirectComposition Window Class";

RegisterClassEx(&wc);

// Creates the m_hMainWindow window.
HWND m_hMainWindow = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW| WS_EX_COMPOSITED,// Extended window style
    wc.lpszClassName,// Name of window class
    L"DirectComposition Layered Child Window Sample",// Title-bar string
    WS_OVERLAPPED | WS_SYSMENU,// Top-level window
    CW_USEDEFAULT,// Horizontal position
    CW_USEDEFAULT,// Vertical position
    1000,// Width
    700,// Height
    NULL,// Parent
    NULL,// Class menu
    GetModuleHandle(NULL),// Handle to application instance
    NULL                                             // Window-creation data
);

if (!m_hMainWindow)
{
    hr = HRESULT_FROM_WIN32(GetLastError());
}

if (SUCCEEDED(hr))
{
    ShowWindow(m_hMainWindow,SW_SHOWDEFAULT);
}


WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WindowProc;
wcex.hCursor = LoadCursor(NULL,IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = L"DirectCompositionChildWindow-Child";

RegisterClassEx(&wcex);

// !!! m_hControlChildWindow is always NULL
HWND m_hControlChildWindow = CreateWindowEx(WS_EX_LAYERED,// Extended window style
    wcex.lpszClassName,// Name of window class
    NULL,// Title-bar string
    WS_CHILD,// Child window
    30,30,100,// Window will be resized via MoveWindow
    m_hMainWindow,// Handle to application instance
    NULL);                                   // Window-creation data

if (!m_hControlChildWindow)
{
    hr = HRESULT_FROM_WIN32(GetLastError());
}
ShowWindow(m_hControlChildWindow,nCmdShow);
UpdateWindow(m_hControlChildWindow);