有没有更好的方法将tvOS的navigationBarTitle排除在外?

时间:2019-11-30 19:08:51

标签: ios swiftui tvos

...要重用带有navigationBarTitle的视图?还是真的要重复所有的事情……只是没有tvos的navigationBarTitle吗?

 #if os(iOS)

    NavigationLink(destination: CountryDetails(countryInfo: self.$userData.countryInfos[countryInfosIndex(of: country.name)])
                                .environmentObject(self.userData)
                                .navigationBarTitle(Text(country.name), displayMode: .inline) // <<<<<<<< the only difference
                        ) {
                            EmptyView() 
                        }.frame(width: 1, height: 1) 
 #else
    NavigationLink(destination: CountryDetails(countryInfo: self.$userData.countryInfos[countryInfosIndex(of: country.name)])
                                .environmentObject(self.userData)
                        ) {
                            // CountryRow(country: country)
                            EmptyView() 
                        }.frame(width: 1, height: 1) 
 #endif

1 个答案:

答案 0 :(得分:1)

尝试这种方法

extension View {
    public func iosnavigationBarTitle(_ title: Text, 
             displayMode: NavigationBarItem.TitleDisplayMode = .inline) -> some View {
#if os(iOS)
        return self.navigationBarTitle(title, displayMode: displayMode)
#else
        return self
#endif
    }
}