我学习了如何在swiftUI中创建类似UIKit tabBar的tabBar。我想将中间的tabItem移到顶部。有什么办法可以实现?
TabView代码
TabView {
ViewTasks()
.tabItem {
Image(systemName: "list.bullet.below.rectangle")
Text("View Tasks")
}
AddTask()
.tabItem {
Image(systemName: "plus.circle").font(.system(size: 60))
}
CategoriesTasks()
.tabItem {
Image(systemName: "square.grid.2x2")
Text("Categories")
}
}
视觉示例
答案 0 :(得分:2)
第一个想法是使用ZStack
,以便您可以用自己的可点击图像覆盖tabItem
。这是示例:
struct TabViewModel: View {
@State var showActionSheet: Bool = false
var body: some View {
ZStack {
GeometryReader { geometry in
TabView {
Text("list")
.tabItem {
Image(systemName: "list.bullet.below.rectangle")
}
Text("plus")
.tabItem {
Text(" ")
}
Text("categories")
.tabItem {
Image(systemName: "square.grid.2x2")
}
}
Image(systemName: "plus.circle")
.resizable()
.frame(width: 40, height: 40)
.shadow(color: .gray, radius: 2, x: 0, y: 5)
.offset(x: geometry.size.width / 2 - 20, y: geometry.size.height - 80)
.onTapGesture {
self.showActionSheet.toggle()
}
}
}
.actionSheet(isPresented: $showActionSheet) {
ActionSheet(title: Text("some actions"))
}
}
}
因此某些图像将覆盖中心的tabView项,该项将不可见(Text(" ")
):
更新
如果您仍然需要在这3个视图之间切换,则可以使用@State var selection
(不要忘记使用tag(...)
tabItems):
struct TabViewModel: View {
@State var selection: Int = 0
var body: some View {
ZStack {
GeometryReader { geometry in
TabView(selection: self.$selection) {
//...
Text("plus")
.tabItem {
Text(" ")
}.tag(1)
//...
.onTapGesture {
self.selection = 1
}
// ...
}
答案 1 :(得分:0)
SWIFTUI 2
//VARIABLES
string gLetter = null, studentName = null;
int test1 = 0, test2 = 0, test3 = 0, avg = 0;
private void processButton_Click(object sender, EventArgs e)
{
try
{
getdata();
calculatedata();
displaydata();
resetitems();
}
catch
{
MessageBox.Show("Error");
}
}
private void getdata()
{
try
{
StreamReader inputFile;
if (openFile.ShowDialog() == DialogResult.OK)
{
inputFile = File.OpenText(openFile.FileName);
while (!inputFile.EndOfStream)
{
studentName = inputFile.ReadLine();
test1 = int.Parse(inputFile.ReadLine());
test2 = int.Parse(inputFile.ReadLine());
test3 = int.Parse(inputFile.ReadLine());
}
}
}
catch (Exception)
{
MessageBox.Show("Error");
}
}
private void calculatedata()
{
try
{
//calc avg
avg = (test1 + test2 + test3) / 3;
//find gLetter
if (avg >= 90)
gLetter = "A";
else if (avg >= 80)
gLetter = "B";
else if (avg >= 70)
gLetter = "C";
else if (avg >= 60)
gLetter = "D";
else if (avg < 60)
gLetter = "F";
}
catch
{
MessageBox.Show("Error");
}
}
private void displaydata()
{
try
{
listBox1.Items.Add(studentName + "\t" + avg.ToString("N2") + "\t" + gLetter);
}
catch (Exception)
{
MessageBox.Show("Error");
}
}
private void resetitems()
{
openFile.FileName = " ";
}
private void exitButton_Click(object sender, EventArgs e)
{
try
{
Application.Exit();
}
catch
{
MessageBox.Show("Error");
}
}
的使用有时会导致意外行为,例如不显示覆盖视图(警报、工作表或全屏)或在您单击另一个选项卡时显示它。更安全的方法是使用 .onTapGesture
方法设置选择值:
.onChange(of:)
}