SwiftUI: How to parse JSON arrays from url using Alamofire? - json

Environments:macOS Ventura Developer Beta 3Xcode 14.0 beta 3MacBook Pro (13 inch M1, 2020)
References: https://www.youtube.com/watch?v=aMes-DVVJg4https://github.com/TuenTuenna/SwiftUI_Alamofire_RandomUser_tutorial/tree/01_Alamofire
Hello. I'm building a simple ESD for macOS using SwiftUI.
I'm gonna get informations from json by my server, and show informations what it got from api like this:
GameESD_View
Game Informations, installation
but it doesn't show anything like this:
Not working
This is my code. You can see entire code here: https://github.com/bmplatina/BitmapMac
game.json (JSON file to decode and parse, watch http://developer.prodbybitmap.com/game.json):
{
"games": [
{
"gameIndex": 0,
"gameTitle": "The Humanity",
"gamePlatform": "Windows, macOS",
"gameEngine": "Unity",
"gameGenre": "어드벤처",
"gameDeveloper": "입학했더니 무한 팀플과 과제가 쌓여버린 건에 대하여",
"gamePublisher": "Bitmap Production",
"isEarlyAccess": true,
"gameReleasedDate": 20211210,
"gameWebsite": "http://prodbybitmap.com/wiki/The%20Humanity",
"gameImageURL": "http://www.prodbybitmap.com/w/images/9/99/TheHumanityPoster1.png",
"gameDescription": "Desc"
},
{
"gameIndex": 1,
"gameTitle": "OX",
"gamePlatform": "Windows",
"gameEngine": "Unreal Engine 5",
"gameGenre": "몰입형 VR 퍼즐 게임",
"gameDeveloper": "Team. Assertive",
"gamePublisher": "ENTER, Bitmap Production",
"isEarlyAccess": true,
"gameReleasedDate": 20220624,
"gameWebsite": "http://prodbybitmap.com/wiki/OX",
"gameImageURL": "http://www.prodbybitmap.com/w/images/f/f9/OX_CMYK.JPG",
"gameDescription": "Desc"
}
]
}
GameESD_View.swift:
import Foundation
import SwiftUI
import URLImage
struct GameESD_View: View {
#State private var searchField: String = ""
#ObservedObject var gameViewmodel = gameInfoViewmodel()
let gameInfoExam = exampleGameInfo()
let columnLayout = Array(repeating: GridItem(), count: 4)
var body: some View {
VStack {
ZStack {
Rectangle()
.fill(Color.init(hex: "4188F1"))
.frame(height: 42)
.shadow(radius: 4)
HStack {
Spacer()
Image("bitmapWebLogo")
.resizable()
.scaledToFit()
.frame(height: 30)
Spacer()
// if true {
// Text("Online")
// }
// else {
// Text("Offline Mode")
// }
TextField("Filter".localized(), text: $searchField)
}
}
ScrollView {
VStack(alignment: .leading) {
Text("Seoul Institute of the Arts Collection")
.font(.largeTitle)
.bold()
.padding([.top, .leading])
Text("collection.")
.padding(.leading)
Divider()
LazyVGrid(columns: columnLayout, alignment: .center, spacing: 2) {
// List(gameViewmodel.gameInfos) { aGameInfos in }
ForEach(0..<gameViewmodel.gameInfos.count, id: \.self) { aGameInfos in
GameButtons(gameViewmodel.gameInfos[aGameInfos])
}
}
Divider()
Text("Other Games")
.font(.largeTitle)
.bold()
.padding([.top, .leading])
Text("여러 창작자의 다양한 인디 컨텐츠.")
.padding(.leading)
}
}
.navigationTitle("Games".localized())
}
}
}
struct GameButtons: View {
#State private var showingPopover = false
var gameInfos: gameInfo
init(_ gameInfos: gameInfo) {
self.gameInfos = gameInfos
}
var body: some View {
VStack { }
Button {
showingPopover = true
} label: {
ZStack {
Image("unknownImage")
.resizable()
.scaledToFit()
.frame(width: 300)
URLImage(URL(string: gameInfos.gameImageURL)!) { image in
image
.resizable()
.scaledToFit()
.frame(width:300)
}
LinearGradient(gradient: Gradient(colors: [.clear, Color.black.opacity(0.5)]), startPoint: .top, endPoint: .bottom).frame(width: 300, height: 424)
VStack(alignment: .leading) {
Spacer()
Text(gameInfos.gameTitle)
.foregroundColor(.white)
.font(Font.largeTitle)
.bold()
Divider()
Text("Dev".localized() + ": " + gameInfos.gameDeveloper)
.foregroundColor(.white)
}
.frame(width:256)
.padding()
}
.cornerRadius(24)
.shadow(radius: 4)
.padding()
}
.buttonStyle(PlainButtonStyle())
.sheet(isPresented: $showingPopover) {
VStack(alignment: .leading) {
HStack {
VStack(alignment: .leading) {
Text("Bitmap Games")
.font(Font.largeTitle)
.bold()
Text("Bitmap Store".localized())
}
Spacer()
Button(action: { showingPopover = false }) {
Image(systemName: "x.circle")
.font(.title2)
}
.padding()
.background(Color.red)
.foregroundColor(.white)
.clipShape(Capsule())
.buttonStyle(PlainButtonStyle())
}
.padding()
GameDetailsView(gameIndex: gameInfos.gameIndex)
}
.frame(width: 1000, height: 600)
.fixedSize()
}
}
}
struct GameDetailsView: View {
#State private var installAlert = false
let gameInfo: exampleGameInfo = exampleGameInfo()
var gameIndex: Int
var body: some View {
Text("Temp")
}
}
#if DEBUG
struct ESD_Previews: PreviewProvider {
static var previews: some View {
GameESD_View()
// digitalArtsFestivalWebView()
}
}
#endif
FetchGameInformation.swift:
import Foundation
import Combine
import Alamofire
struct gameInfo: Codable, Identifiable {
var id = UUID()
var gameIndex: Int
var gameTitle: String
var gamePlatform: String
var gameEngine: String
var gameGenre: String
var gameDeveloper: String
var gamePublisher: String
var isEarlyAccess: Bool
var gameReleasedDate: Int
var gameWebsite: String
var gameImageURL: String
var gameDescription: String
var gameImage: URL {
get {
URL(string: gameImageURL)!
}
}
static func makeDummy() -> Self {
print(#fileID, #function, #line, "")
return gameInfo(gameIndex: 1, gameTitle: "OX", gamePlatform: "Windows", gameEngine: "Unreal Engine 5", gameGenre: "몰입형 VR 퍼즐 게임", gameDeveloper: "Team. Assertive", gamePublisher: "ENTER, Bitmap Production", isEarlyAccess: true, gameReleasedDate: 20220624, gameWebsite: "http://prodbybitmap.com/wiki/OX", gameImageURL: "http://www.prodbybitmap.com/w/images/f/f9/OX_CMYK.JPG", gameDescription: "This is Description")
}
}
struct gameResult: Codable, CustomStringConvertible {
var games: [gameInfo]
var description: String {
return "gameInfo.count: \(games.count)"
}
}
struct gamesResponse: Codable, CustomStringConvertible {
var games: [gameInfo]
var description: String {
return "games.count: \(games.count) / info : \(games[0].gameTitle)"
}
}
class gameInfoViewmodel: ObservableObject {
// MARK: Properties
var subscription = Set<AnyCancellable>()
#Published var gameInfos = [gameInfo]()
var url = "http://developer.prodbybitmap.com/game.json"
init() {
print(#fileID, #function, #line, "")
fetchGameInfo()
}
func fetchGameInfo() {
print(#fileID, #function, #line, "")
AF.request(url, method: .get)
.publishDecodable(type: gamesResponse.self)
.compactMap { $0.value }
.map { $0.games }
.sink(receiveCompletion: { completion in
print("Datastream Done")
}, receiveValue: { [weak self](receivedValue: [gameInfo]) in
guard let self = self else { return }
print("Data Value: \(receivedValue.description)")
self.gameInfos = receivedValue
}).store(in: &subscription)
}
}

Related

Some MapKit questions in SwiftUI

I'm looking to make a setting view in my app where I could define 3 map parameters :
Map center initial position
Map initial zoom
Map initial type
Then show an example map and save in into the #AppStorage
My initial position is determined from an array of data which include :
Place name
Place Latitude
Place Longitude
Latitude and Longitude is find via a function which return a Double? :
func getApLat(ApName: String) -> Double?{
guard let foundAirport = FR_airportsDB.first(where: {$0.Icao == ApName}),
let lat = Double(foundAirport.Latitude) else { return nil }
return lat
}
func getApLong(ApName: String) -> Double?{
guard let foundAirport = FR_airportsDB.first(where: {$0.Icao == ApName}),
let longit = Double(foundAirport.Longitude) else { return nil }
return longit
}
Now my settingMapView is defined as follow :
struct MapOptionView: View {
#State private var showingAlert = false
#State private var latDouble = getApLat(ApName: UserDefaults.standard.string(forKey: "MAP_CenterInit") ?? "LFLI")
#State private var longDouble = getApLong(ApName: UserDefaults.standard.string(forKey: "MAP_CenterInit") ?? "LFLI")
#State private var typeExemple: MKMapType = getTypeFromUD()
#State private var exampleZoom: Int = 5
#AppStorage("MAP_CenterInit") private var MapCenterAirport = ""
#AppStorage("MAP_ZoomInit") private var MapZoom = 2
#AppStorage("MAP_TypeInit") private var MapType = 1
var body: some View {
List{
Section(header: Text("Initial location")){
HStack{
TextField("ICAO", text: $MapCenterAirport)
.padding()
.background(.white)
.cornerRadius(20.0)
.keyboardType(.default)
.textCase(.uppercase)
.onReceive(Just(MapCenterAirport)) { inputValue in
if inputValue.count > 4 {
self.MapCenterAirport.removeLast()
}
}
.disableAutocorrection(true)
.textCase(.uppercase)
if MapCenterAirport != "" {
Button {
//Here I update the center data after get it from the function
latDouble = getApLat(ApName: MapCenterAirport)
longDouble = getApLong(ApName: MapCenterAirport)
} label: {
Text("Check")
}
}
}
}
// Section where Zoom is defined
Section(header: Text("Initial zoom")){
HStack {
if MapZoom == 1{
Text("Zoom : Low")
}else if MapZoom == 2{
Text("Zoom : Medium")
}else if MapZoom == 3{
Text("Zoom : Large")
}
Spacer()
Stepper("", value: $MapZoom, in: 1...3)
}.padding(.vertical)
}
// Section where Type is defined
Section(header: Text("Initial type")){
HStack {
if MapType == 1{
Text("Type : Standard")
}else if MapType == 2{
Text("Type : Satellite")
}else if MapType == 3{
Text("Type : Satellite-flyover")
}else if MapType == 4{
Text("Type : Hybrid")
}else if MapType == 5{
Text("Type : Hybrid-flyover")
}
Spacer()
Stepper("", value: $MapType, in: 1...5)
.frame(width: 60)
.padding(.horizontal, 20)
}.padding(.vertical)
}
// Section where Exemple is Show but need to clickButton to update map ...
Section(header: Text("Example")){
Button {
//Here is zoom adaptation regarding stepper choice from $MapZoom
if MapZoom == 1{
exampleZoom = 60000
}else if MapZoom == 2{
exampleZoom = 110000
}else if MapZoom == 3{
exampleZoom = 160000
}
//Here is type adaptation regarding stepper choice from $MapType
if MapType == 1{
typeExemple = .standard
}else if MapType == 2{
typeExemple = .satellite
}else if MapType == 3{
typeExemple = .satelliteFlyover
}else if MapType == 4{
typeExemple = .hybrid
}else if MapType == 5{
typeExemple = .hybridFlyover
}
} label: {
HStack {
Text("Update example")
}
}
//Here is my Map definition
MapViewSetting(mapType: $typeExemple,
funcLat: $latDouble,
funcLong: $longDouble,
funcZoom: $exampleZoom
)
.edgesIgnoringSafeArea(.all)
.frame(height: 300)
}
}.navigationTitle("Map options")
.navigationBarItems(trailing: Button(action: {
showingAlert = true
//Here we save the 3 value into the AppStorage
MapCenterAirport = MapCenterAirport.uppercased()
UserDefaults.standard.set(self.MapCenterAirport, forKey: "MAP_CenterInit")
UserDefaults.standard.set(self.MapZoom, forKey: "MAP_ZoomInit")
UserDefaults.standard.set(self.MapType, forKey: "MAP_TypeInit")
}, label: {
HStack {
Text("Save")
}
})).alert("Informations saved !\nMust have to restart the app to apply it.", isPresented: $showingAlert) {
Button("OK", role: .cancel) { }
}
}
}
And finally I've my map struct defined as follow after following some post find on this forum.
struct MapViewSetting: UIViewRepresentable {
#Binding var mapType: MKMapType
#Binding var funcLat: Double
#Binding var funcLong: Double
#Binding var funcZoom: Int
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView(frame: .zero)
let center = CLLocationCoordinate2D(latitude: funcLat, longitude: funcLong)
let region = MKCoordinateRegion(center: center,
latitudinalMeters: CLLocationDistance(funcZoom),
longitudinalMeters: CLLocationDistance(funcZoom)
)
mapView.setRegion(region, animated: true)
mapView.mapType = mapType
mapView.showsScale = true
mapView.showsTraffic = false
mapView.showsCompass = true
mapView.showsUserLocation = false
mapView.showsBuildings = false
return mapView
}
func updateUIView(_ view: MKMapView, context: Context) {
view.mapType = self.mapType
}
}
But Two big mistake with my code :
First I got an error here :
MapViewSetting(mapType: $typeExemple,
/*regionFunc: $region,*/
funcLat: $latDouble ?? 46.192001,
funcLong: $longDouble ?? 6.26839,
funcZoom: $exampleZoom
)
On funcLat : and funcLong which say : Cannot convert value of type 'Binding<Double?>' to expected argument type 'Binding<Double>'
And If I define manually the Lat and Long in my code to test the two other parameters I could only change the type but not the Zoom ...
Hope to be as clear as I can
Thanks

navigate after eight seconds from pressing a button

this is my button
Button(action: {
SearchSomeone()
},label: {
NavigationLink(destination: mySearchList()){
HStack(alignment: .center) {
Text("Search")
.font(.system(size: 17))
.fontWeight(.bold)
.foregroundColor(.white)
.frame(minWidth: 0, maxWidth: .infinity)
.padding()
.background(
RoundedRectangle(cornerRadius: 25)
.fill(Color("Color"))
.shadow(color: .gray, radius: 2, x: 0, y: 2)
)
}
and this button does the function and search together at the same time and since search would take time so I won't see the list, how can I do the function and then after 8 seconds I do the navigation after it ? thank you
According to the information, you'd like to switch to a new view after 8 seconds. This code should work for you.
import SwiftUI
struct ContentView: View {
//Variable to see if view should change or not
#State var viewIsShowing = false
var body: some View {
//Detecting if variable is false
if viewIsShowing == false {
//Showing a button that sets the variable to true
Button(action: {
//Makes it wait 8 seconds before making the variable true
DispatchQueue.main.asyncAfter(deadline: .now() + 8.0) {
viewIsShowing = true
}
}) {
//Button text
Text("Search")
.font(.system(size: 17))
.fontWeight(.bold)
.frame(minWidth: 0, maxWidth: .infinity)
.padding()
.background(
RoundedRectangle(cornerRadius: 25)
.fill(Color("Color"))
.shadow(color: .gray, radius: 2, x: 0, y: 2)
)
}
} else {
//If the variable equals false, go here
View2()
}
}
}
//Your other view you want to go to
struct View2: View {
var body: some View {
Text("abc")
}
}

_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List<dynamic>>

hi I am still learning flutter and I am struggling to understand and fix this problem.
I am using dio to retrieve data from api to pass it to repository then to the cubit and then the ui.
when ever I press button that takes me to screen that suppose to show data that come api.
gives me this error.
debug counsel
flutter: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'FutureOr<List<dynamic>>'
Json respons
{
"chapters": [
{
"id": 114,
"revelation_place": "makkah",
"revelation_order": 21,
"bismillah_pre": true,
"name_simple": "An-Nas",
"name_complex": "An-Nās",
"name_arabic": "الناس",
"verses_count": 6,
"pages": [
604,
604
],
"translated_name": {
"language_name": "english",
"name": "Mankind"
}
}
]}
my model
class ChapterModel {
List<Chapters>? chapters;
ChapterModel({this.chapters});
ChapterModel.fromJson(Map<String, dynamic> json) {
if (json['chapters'] != null) {
chapters = <Chapters>[];
json['chapters'].forEach((v) {
chapters!.add(new Chapters.fromJson(v));
});
}
}
}
class Chapters {
int? id;
String? revelationPlace;
int? revelationOrder;
bool? bismillahPre;
String? nameSimple;
String? nameComplex;
String? nameArabic;
int? versesCount;
List<int>? pages;
TranslatedName? translatedName;
Chapters(
{this.id,
this.revelationPlace,
this.revelationOrder,
this.bismillahPre,
this.nameSimple,
this.nameComplex,
this.nameArabic,
this.versesCount,
this.pages,
this.translatedName});
Chapters.fromJson(Map<String, dynamic> json) {
id = json['id'];
revelationPlace = json['revelation_place'];
revelationOrder = json['revelation_order'];
bismillahPre = json['bismillah_pre'];
nameSimple = json['name_simple'];
nameComplex = json['name_complex'];
nameArabic = json['name_arabic'];
versesCount = json['verses_count'];
pages = json['pages'].cast<int>();
translatedName = json['translated_name'] != null
? new TranslatedName.fromJson(json['translated_name'])
: null;
}
}
my dio
class ApinWebServices {
static late Dio dio;
static init() {
BaseOptions options = BaseOptions(
baseUrl: baseUrl,
queryParameters: {'language': 'ar'},
receiveDataWhenStatusError: true,
);
dio = Dio(options);
}
Future<List<dynamic>> getALLJuzQuran() async {
try {
Response response = await dio.get('chapters');
return response.data;
} catch (e) {
print(e.toString());
return [];
}
}
}
my repository
class ChapterRepository {
final ApinWebServices apinWebServices;
ChapterRepository(this.apinWebServices);
Future<List<ChapterModel>> getALLJuzQuran() async {
final juzQuran = await apinWebServices.getALLJuzQuran();
return juzQuran.map((e) => ChapterModel.fromJson(e)).toList();
}
}
my cubit
class JuzquranCubit extends Cubit<HomeState> {
final ChapterRepository chapterRepository;
late List<ChapterModel> juzQuran = [];
JuzquranCubit(this.chapterRepository) : super(HomeInitial());
List<ChapterModel> getALLJuzQuran() {
chapterRepository.getALLJuzQuran().then((chapters) {
emit(JuzQuranLoaded(chapters: chapters));
this.juzQuran = juzQuran;
});
return juzQuran;
}
}
In your ApinWebServices, you are using
Dio.get('chapters');
which is not a property of Dio, as your data is json use
return response.data['chapters'];

For loop returning only first json Object Flutter

While I tried to fetch data from a JSON am only able to return the first object.
I had mentioned the statement which I think the issue is. I want to return all the available object from the JSON so that I shall display it in a list of cards. I need to return all the JSON objects and pass it to another page containing list of cards. If anyone knows the solution please help me fix it.
Thanks in advance.
import 'dart: convert';
import 'package:flutter/cupertino.dart';
import 'package:tts/tts.dart';
import 'package:wiizboo/service/Chatmsg_service.dart';
import 'package:wiizboo/widget/a%20copy.dart';
import 'package:wiizboo/widget/chat_in_message.dart';
import 'package:wiizboo/widget/chat_out_message.dart';
import 'package:wiizboo/widget/form.dart';
import 'package:wiizboo/widget/image_camera.dart';
import 'package:wiizboo/widget/image_gallery.dart';
class ChatMessageModel with ChangeNotifier {
String data;
List accntdet;
ChatmessageService chatservice = ChatmessageService();
List<Widget> messages = <Widget>[];
ChatMessageModel() {
sendMsg("Hi");
}
Future handlesubmission(String chattext) {
Widget message = new ChatInMessage(
text: chattext,
name: "Me",
type: true,
);
addMsg(message);
sendMsg(chattext);
}
addMsg(Widget msg) {
messages.insert(0, msg);
notifyListeners();
}
sendMsg(String msg) {
chatservice.SendMsg(msg).then((String msg) {
responseBuilder(msg);
});
}
responseBuilder(String msg) {
Widget message;
String identifier = '';
var arr = msg.split("~");
if (arr.length > 1) {
identifier = arr[0];
msg = arr[1];
} else {
msg = arr[0];
}
switch (identifier) {
case 'email_phone':
message = new Forms(onSubmitted: (String val) {
Widget a = new ChatInMessage(
text: val,
name: "Me",
type: true,
);
addMsg(a);
sendMsg(val);
});
break;
case 'selfie':
message = new ImageCamera(onSubmitted: (String imageres) {
Widget b = new ChatInMessage(
text: imageres,
name: "Me",
type: true,
);
sendMsg(imageres);
});
break;
case 'aadhar':
message = new ImageGalery(onSubmitted: (String imageres) {
Widget b = new ChatInMessage(
text: imageres,
name: "Me",
type: true,
);
sendMsg(imageres);
});
break;
case 'account_info':
print(msg);
data = msg;
String receivedJson = data;
List<dynamic> list = json.decode(receivedJson);
accntdet = list;
int l = list.length;
print(l);
//------------ the statement --------//
for (dynamic account in accntdet) {
message = new AccountInfo(
l: l,
iban: account['ibn_no'],
accno: account['account_no'],
sort: account['sort-code'],
currency: account['currency'],
);
}
//----------//
print(message);
break;
default:
message = new ChatOutMessage(
text: msg,
name: "WzBoo..",
);
Tts.speak(msg);
}
addMsg(message);
}
}
Change this bloc
class ChatMessageModel with ChangeNotifier {
String data;
List accntdet;
ChatmessageService chatservice = ChatmessageService();
List<Widget> messages = <Widget>[];
ChatMessageModel() {
sendMsg("Hi");
}
Future handlesubmission(String chattext) {
Widget message = new ChatInMessage(
text: chattext,
name: "Me",
type: true,
);
addMsg(message);
sendMsg(chattext);
}
addMsg(Widget msg) {
messages.insert(0, msg);
notifyListeners();
}
sendMsg(String msg) {
chatservice.SendMsg(msg).then((String msg) {
responseBuilder(msg);
});
}
responseBuilder(String msg) {
Widget message;
String identifier = '';
var arr = msg.split("~");
if (arr.length > 1) {
identifier = arr[0];
msg = arr[1];
} else {
msg = arr[0];
}
switch (identifier) {
case 'email_phone':
message = new Forms(onSubmitted: (String val) {
Widget a = new ChatInMessage(
text: val,
name: "Me",
type: true,
);
addMsg(a);
sendMsg(val);
});
break;
case 'selfie':
message = new ImageCamera(onSubmitted: (String imageres) {
Widget b = new ChatInMessage(
text: imageres,
name: "Me",
type: true,
);
sendMsg(imageres);
});
addMsg(message);
break;
case 'aadhar':
message = new ImageGalery(onSubmitted: (String imageres) {
Widget b = new ChatInMessage(
text: imageres,
name: "Me",
type: true,
);
sendMsg(imageres);
});
break;
case 'account_info':
print(msg);
data = msg;
String receivedJson = data;
List<dynamic> list = json.decode(receivedJson);
accntdet = list;
int l = list.length;
print(l);
//------------ the statement --------//
for (dynamic account in accntdet) {
message = new AccountInfo(
l: l,
iban: account['ibn_no'],
accno: account['account_no'],
sort: account['sort-code'],
currency: account['currency'],
);
print(message);
addMsg(message);
}
//----------//
break;
default:
message = new ChatOutMessage(
text: msg,
name: "WzBoo..",
);
Tts.speak(msg);
addMsg(message);
}
}
}

There doesn't appear to be support for UITabBar in SwiftUI. Workarounds?

SwiftUI doesn't appear to support UITabBar. How can I integrate that capability?
Merely wrapping the view like one would a (eg) MKMapView, doesn't work because of its need for deep integration with NavigationView. Using UINavigationView is too un-SwiftUI-ish.
The 'TabbedView' is the closest thing. It can be used similar to the following:
struct TabView : View {
#State private var selection = 1
var body: some View {
TabbedView (selection: $selection) {
InboxList()
.tabItemLabel(selection == 1 ? Image("second") : Image("first"))
.tag(1)
PostsList()
.tabItemLabel(Image("first"))
.tag(2)
Spacer()
.tabItemLabel(Image("first"))
.tag(3)
Spacer()
.tabItemLabel(Image("second"))
.tag(4)
}
}
}
If you aren't happy with TabbedView, you can always roll your own! Here's a quick base implementation:
import SwiftUI
struct ContentView : View {
let tabs = [TabItemView(title: "Home", content: { Text("Home page text") }), TabItemView(title: "Other", content: { Text("Other page text") }), TabItemView(title: "Pictures", content: { Text("Pictures page text") })]
var body: some View {
TabBar(tabs: tabs, selectedTab: tabs[0])
}
}
struct TabItemView<Content> : Identifiable where Content : View {
var id = UUID()
var title: String
var content: Content
init(title: String, content: () -> Content) {
self.title = title
self.content = content()
}
var body: _View { content }
typealias Body = Never
}
struct TabBar<Content>: View where Content : View {
let tabButtonHeight: Length = 60
var tabs: [TabItemView<Content>]
#State var selectedTab: TabItemView<Content>
var body: some View {
GeometryReader { geometry in
VStack(spacing: 0) {
self.selectedTab.content.frame(width: geometry.size.width, height: geometry.size.height - self.tabButtonHeight)
Divider()
HStack(spacing: 0) {
ForEach(self.tabs) { tab in
Button(action: { self.selectedTab = tab}) {
Text(tab.title)
}.frame(width: geometry.size.width / CGFloat(Double(self.tabs.count)), height: self.tabButtonHeight)
}
}
.background(Color.gray.opacity(0.4))
}
.frame(width: geometry.size.width, height: geometry.size.height)
}
}
}
UITabBar seems to be working now on Xcode 13.3, SwiftUI 3, iOS15+
It works even though I didn't import UIKit, not sure if that has any effect but it's working for me
struct LandingView: View {
#Binding var selectedTab: String
//hiding tab bar
init(selectedTab: Binding<String>) {
self._selectedTab = selectedTab
UITabBar.appearance().isHidden = true
}
var body: some View {
//Tab view with tabs
TabView(selection: $selectedTab) {
//Views
Home()
.tag("Home")
PlaylistView()
.tag("My Playlists")
HistoryView()
.tag("History")
}
}
}
I misstated the question as I was trying to make ToolBar... below is the code I ended up with... thanks to all.
struct ToolBarItem : Identifiable {
var id = UUID()
var title : String
var imageName : String
var action: () -> Void
}
struct TooledView<Content> : View where Content : View{
var content : Content
var items : [ToolBarItem]
let divider = Color.black.opacity(0.2)
init(items : [ToolBarItem], content: () -> Content){
self.items = items
self.content = content()
}
var body : some View{
VStack(spacing: 0){
self.content
self.divider.frame(height: 1)
ToolBar(items: self.items).frame(height: ToolBar.Height)
}
}
}
struct ToolBar : View{
static let Height : Length = 60
var items : [ToolBarItem]
var body: some View {
GeometryReader { geometry in
HStack(spacing: 0){
ForEach(self.items){ item in
Button(action: item.action){
Image(systemName: item.imageName).imageScale(.large)
Text(item.title).font(.caption)
}.frame(width: geometry.size.width / CGFloat(Double(self.items.count)))
}
}
.frame(height: ToolBar.Height)
.background(Color.gray.opacity(0.10))
}
}
}