Swiftui可重复使用的视图
我有一个“ TopTabbar”视图,并且在两个不同的屏幕中使用它。但是在一个屏幕中,应该有2个按钮,而在另一个屏幕中应该有4个按钮。 在通常的Swift中,我将在此TopTabbar中创建一个函数,并将其添加到堆栈中,并从我使用的地方创建它们。但是在Swiftui中,我做不到。 我如何重新设置屏幕并从外部填充适量的按钮?
import SwiftUI
struct TopBar: View {
var firstViewTitle: String
var secondViewTitle: String
var thirdViewTitle: String?
var fourthViewTitle: String?
@Binding var tabIndex: Int
var body: some View {
HStack(spacing: 0) {
TabBarButton(text: firstViewTitle, isSelected: .constant(tabIndex == 0))
.onTapGesture { onButtonTapped(index: 0) }
TabBarButton(text: secondViewTitle, isSelected: .constant(tabIndex == 1))
.onTapGesture { onButtonTapped(index: 1) }
TabBarButton(text: thirdViewTitle ?? "", isSelected: .constant(tabIndex == 2))
.onTapGesture { onButtonTapped(index: 2) }
TabBarButton(text: fourthViewTitle ?? "", isSelected: .constant(tabIndex == 3))
.onTapGesture { onButtonTapped(index: 3) }
}
.border(width: 1, edges: [.bottom], color: .lightGrey)
.frame(width: UIScreen.screenWidth, height: 50, alignment: .center)
}
private func onButtonTapped(index: Int) {
withAnimation { tabIndex = index }
}
}
struct TabBarButton: View {
let text: String
@Binding var isSelected: Bool
var body: some View {
Text(text)
.foregroundColor(isSelected ? .black : .gray)
.fontWeight(isSelected ? .heavy : .regular)
.padding(.bottom, 10)
.border(width: isSelected ? 2 : 0, edges: [.bottom], color: .black)
.frame(width: UIScreen.screenWidth/4, height: 50, alignment: .center)
.background(Color.random)
}
}
struct Usable: View {
@State var tabIndex = 0
var body: some View {
NavigationView {
ZStack {
if tabIndex == 1 {
debugPring("xxx")
}
VStack {
TopBar(firstViewTitle: "first", secondViewTitle: "second", thirdViewTitle: "third", fourthViewTitle: "fourth" ,tabIndex: $tabIndex).padding(.top, 20)
if tabIndex == 0 {
debugPrint("xxx")
}
Spacer()
}
}
.navigationBarHidden(true)
}.accentColor(Color.black)
}
}
i have a view "TopTabBar" and i use it in two different screens. but in one screen there should be 2 buttons, and in the other 4.
in the usual swift, I would create a function in this TopTabBar, with which I would add buttons to the Stack, and create them from where I use it. but in SwiftUI I can't do that.
How can I get the screen reused and filled with the right amount of buttons from the outside?
import SwiftUI
struct TopBar: View {
var firstViewTitle: String
var secondViewTitle: String
var thirdViewTitle: String?
var fourthViewTitle: String?
@Binding var tabIndex: Int
var body: some View {
HStack(spacing: 0) {
TabBarButton(text: firstViewTitle, isSelected: .constant(tabIndex == 0))
.onTapGesture { onButtonTapped(index: 0) }
TabBarButton(text: secondViewTitle, isSelected: .constant(tabIndex == 1))
.onTapGesture { onButtonTapped(index: 1) }
TabBarButton(text: thirdViewTitle ?? "", isSelected: .constant(tabIndex == 2))
.onTapGesture { onButtonTapped(index: 2) }
TabBarButton(text: fourthViewTitle ?? "", isSelected: .constant(tabIndex == 3))
.onTapGesture { onButtonTapped(index: 3) }
}
.border(width: 1, edges: [.bottom], color: .lightGrey)
.frame(width: UIScreen.screenWidth, height: 50, alignment: .center)
}
private func onButtonTapped(index: Int) {
withAnimation { tabIndex = index }
}
}
struct TabBarButton: View {
let text: String
@Binding var isSelected: Bool
var body: some View {
Text(text)
.foregroundColor(isSelected ? .black : .gray)
.fontWeight(isSelected ? .heavy : .regular)
.padding(.bottom, 10)
.border(width: isSelected ? 2 : 0, edges: [.bottom], color: .black)
.frame(width: UIScreen.screenWidth/4, height: 50, alignment: .center)
.background(Color.random)
}
}
struct Usable: View {
@State var tabIndex = 0
var body: some View {
NavigationView {
ZStack {
if tabIndex == 1 {
debugPring("xxx")
}
VStack {
TopBar(firstViewTitle: "first", secondViewTitle: "second", thirdViewTitle: "third", fourthViewTitle: "fourth" ,tabIndex: $tabIndex).padding(.top, 20)
if tabIndex == 0 {
debugPrint("xxx")
}
Spacer()
}
}
.navigationBarHidden(true)
}.accentColor(Color.black)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上有很多选择,但是有了您当前的设计,可以使它们有条件,例如
Actually there are many options, but with your current design it is possible just to make them conditional, like