Parsing Video from JSON to Swift ( SwiftUI) - ios

i'm somewhat new to working with JSON data in Swift so I have a problem with it.
I did fetch all the vars from JSON into SwiftUI, working really well, except I don't know how to decode videos from JSON and add them to SwiftUI
From what I see in the JSON there is a Video Endpoint
( ex. base]/uploads/companymedia/[id]/[value].mp4", )
But, im not sure how to decode it, and put it so it works in my SwiftUI file. Currently its fetching everything ( text, subtext, images ) just not videos and on the part where the video should be it just shows an image.
The code is this:
HStack{
Text(post.title)
.font(.system(size: 18))
.fontWeight(.heavy)
.foregroundColor(Color(.label))
Spacer()
Button(action: shareButton){
Image(systemName: "square.and.arrow.up")
.font(.system(size: 22))
.foregroundColor(Color.gray)
.padding(.top, 15)
.padding(.bottom, 20)
.padding(.horizontal, 15)
}
}
if let text = post.text{
Text(text)
.lineLimit(2)
.foregroundColor(Color(.label).opacity(0.5))
}
Text(dateFormatter.string(from: post.date))
.font(.system(size: 14))
.foregroundColor(Color(.label).opacity(0.5))
}
.padding(16)
GeometryReader{ geometry in
PagedScrollView(numberOfPages: post.images.count){
HStack(spacing: 0){
ForEach(post.images, id: \.self){ image in
KFImage(URL(string: image)!)
.setProcessor(processor)
.cacheOriginalImage(true)
.backgroundDecode(true)
.loadImmediately()
.fade(duration: 1)
.placeholder({
Color.red
})
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: geometry.size.width, height: geometry.size.height)
.clipped()
}
}
}
.frame(width: geometry.size.width, height: geometry.size.height)
.tabViewStyle(PageTabViewStyle())
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
}
.frame(height: 400)
HStack{
Locallike(isLiked: $isLiked)
.padding(.horizontal, 10)
.padding(.top,15)
.padding(.bottom, 20)
SmileLike(isSmiled: $isSmiled)
.font(.system(size: 22))
.padding(.top, 15)
.padding(.bottom, 20)
.padding(.horizontal,15)
Spacer()
NavigationLink(destination: CommentsView(postId: post.id)) {
Image(systemName: "message")
.font(.system(size: 22))
.foregroundColor(Color.gray)
.padding(.top, 15)
.padding(.bottom, 20)
.padding(.horizontal,15)
}
}
}
.background(Color(.label).opacity(0.1))
.cornerRadius(8)
}
```

Related

How do i align text to the bottom of a VStack that has a frame?

I am creating a view that is a rectangle that contains an: image, name, subtitle.
I have these in a VStack with a frame. In this VStack, the views stack correctly. However, I want the subtitle (which is text) to always be stuck to the bottom of the VStack.
At the moment the subtitle is pushed up with the other views. I tried using Spacer's but these didn't seem to do the job (maybe I used them wrong???).
Here is the view:
VStack(alignment: .center, spacing: 0) {
Image("Image")
.resizable()
.aspectRatio(contentMode: .fit)
.clipped()
.padding()
.layoutPriority(1)
}
}
Text("Name")
.font(.title2)
.fontWeight(.bold)
.foregroundColor(.black)
.minimumScaleFactor(0.5)
.multilineTextAlignment(.center)
.padding(.leading, 5)
.padding(.trailing, 5)
.padding(.bottom, 5)
Text("Subtitle")
.font(.caption)
.foregroundColor(.gray)
.lineLimit(2)
.padding(1)
}
.frame(width: 150, height: 200)
Any ideas?
Put the subtitle into an .overlay() of the VStack and use alignment: .bottom to push it to the bottom:
VStack(alignment: .center, spacing: 0) {
Image("Image")
.resizable()
.aspectRatio(contentMode: .fit)
.clipped()
.padding()
.layoutPriority(1)
Text("Name")
.font(.title2)
.fontWeight(.bold)
.foregroundColor(.black)
.minimumScaleFactor(0.5)
.multilineTextAlignment(.center)
.padding(.leading, 5)
.padding(.trailing, 5)
.padding(.bottom, 5)
}
.frame(width: 150, height: 200)
.overlay(alignment: .bottom) {
Text("Subtitle")
.font(.caption)
.foregroundColor(.gray)
.lineLimit(2)
.padding(1)
}

How do i align an image with text in an HStack?

I am trying to create a view that is a rectangle with text and images inside. This is done in a VStack. At the bottom of the VStack, I have an HStack where I have a small image and a number next to it. These need to be centred aligned but I can't seem to make that happen.
The image is higher up than the number. I need them to be at an equal height.
VStack(alignment: .center, spacing: 0) {
Image("ProductLogo")
.resizable()
.aspectRatio(contentMode: .fit)
.clipped()
.padding()
.layoutPriority(1)
Text("Text Here")
.font(.title2)
.fontWeight(.bold)
.foregroundColor(.black)
.minimumScaleFactor(0.5)
.multilineTextAlignment(.center)
.padding(.leading, 5)
.padding(.trailing, 5)
.padding(.bottom, 5)
Text("Subtitle")
.font(.headline)
.foregroundColor(.gray)
.lineLimit(2)
.padding(1)
//HERE IS WHAT NEEDS TO BE ALIGNED
HStack {
Image("CoinImage")
.resizable()
.frame(width: 35, height: 35, alignment: .center)
Text("500")
.font(.system(size: 20))
.padding(.top,20)
}
//END OF HSTACK
}
Here is an image of the VStack:
The 500 should be level with the coin image
You just need to remove padding from Text("500") and set to HStack
Please try with below code and let me know it's works for you
VStack(alignment: .center, spacing: 0) {
Image("ProductLogo")
.resizable()
.aspectRatio(contentMode: .fit)
.clipped()
.padding()
.layoutPriority(1)
Text("Text Here")
.font(.title2)
.fontWeight(.bold)
.foregroundColor(.black)
.minimumScaleFactor(0.5)
.multilineTextAlignment(.center)
.padding(.leading, 5)
.padding(.trailing, 5)
.padding(.bottom, 5)
Text("Subtitle")
.font(.headline)
.foregroundColor(.gray)
.lineLimit(2)
.padding(1)
//HERE IS WHAT NEEDS TO BE ALIGNED
HStack {
Image("CoinImage")
.resizable()
.frame(width: 35, height: 35, alignment: .center)
Text("500")
.font(.system(size: 20))
}.padding(.top, 10)
//END OF HSTACK
}

Stacking a HStack below a ZStack in SwiftUI

Disclaimer: New to SwiftUI
I know a ZStack() view allows me to overlap views, which is what I want for the heading section of the app.
I set the ZStack() to not take up the entire height and had expected an HStack(), placed after the ZStack() to do just that, appear after. Instead it also overlaps with the ZStack.
I'm sure it's a simple solution to co-exist. Image and code below.
var body: some View {
GeometryReader { geometry in
ZStack(alignment: .topLeading) {
Ellipse()
.fill(self.bgColor)
.frame(width: geometry.size.width * 1.4, height: geometry.size.height * 0.28)
.position(x: geometry.size.width / 2.35, y: geometry.size.height * 0.1)
.shadow(radius: 3)
.edgesIgnoringSafeArea(.all)
HStack(alignment: .top) {
VStack(alignment: .leading) {
Text(self.title)
.font(.title)
.fontWeight(.bold)
.foregroundColor(Color.white)
Text(self.subtitle)
.font(.subheadline)
.fontWeight(.regular)
.foregroundColor(Color.white)
}
.padding(.leading, 25)
.padding(.top, 20)
Spacer()
VStack(alignment: .trailing) {
Image("SettingsIcon")
.resizable()
.scaledToFit()
.frame(width: 30, height: 30)
}
.padding(.top, 20)
.padding(.trailing, 25)
}
}
.fixedSize(horizontal: false, vertical: true)
HStack(alignment: .top) {
Text(self.title)
.font(.title)
.fontWeight(.bold)
.foregroundColor(Color.white)
}
}
}
Try the following code using a top VStack and closing the GeometryReader before the last HStack:
var body: some View {
VStack { // <-- here
GeometryReader { geometry in
ZStack(alignment: .topLeading) {
Ellipse()
.fill(self.bgColor)
.frame(width: geometry.size.width * 1.4, height: geometry.size.height * 0.28)
.position(x: geometry.size.width / 2.35, y: geometry.size.height * 0.1)
.shadow(radius: 3)
.edgesIgnoringSafeArea(.all)
HStack(alignment: .top) {
VStack(alignment: .leading) {
Text(self.title)
.font(.title)
.fontWeight(.bold)
.foregroundColor(Color.white)
Text(self.subtitle)
.font(.subheadline)
.fontWeight(.regular)
.foregroundColor(Color.white)
}
.padding(.leading, 25)
.padding(.top, 20)
Spacer()
VStack(alignment: .trailing) {
Image("SettingsIcon")
.resizable()
.scaledToFit()
.frame(width: 30, height: 30)
}
.padding(.top, 20)
.padding(.trailing, 25)
}
}.fixedSize(horizontal: false, vertical: true)
} // <-- here end GeometryReader
HStack(alignment: .top) {
Text(self.title)
.font(.title)
.fontWeight(.bold)
.foregroundColor(Color.white)
}
} // <-- here end VStack
}

How to avoid View moving off the screen when keyboard appears in SwiftUI?

absolute greenhorn writing here:
i have a very basic app that does some calculations. It has only one view that has some Stacks in it that then contain text fields. underneath are two buttons and an image. when I now click on a text field, the view moves up and off the screen even though I want to make the keyboard over the buttons and image as I do not need them visible when the keyboard is active. I have tried to apply the .ignoresSafeArea(.keyboard)addition to basically everything in the view but it still does it.
Screenshot with Keyboard up
VStack{
Spacer()
HStack (alignment: .top){
VStack (alignment: .trailing, spacing:8){
Text("Volumenstrom")
.font(.body)
.fontWeight(.bold)
.multilineTextAlignment(.center)
.frame(height: 50)
.ignoresSafeArea(.keyboard)
Text("Geschwindigkeit")
.font(.body)
.fontWeight(.bold)
.multilineTextAlignment(.center)
.frame(height: 50)
.ignoresSafeArea(.keyboard)
Text("Kanalmaß A")
.font(.body)
.fontWeight(.bold)
.multilineTextAlignment(.center)
.frame(height: 50)
.ignoresSafeArea(.keyboard)
Text("Kanalmaß B")
.font(.body)
.fontWeight(.bold)
.multilineTextAlignment(.center)
.frame(height: 50)
.ignoresSafeArea(.keyboard)
Text("Durchmesser D")
.font(.body)
.fontWeight(.bold)
.multilineTextAlignment(.center)
.frame(height: 50)
.ignoresSafeArea(.keyboard)
}
VStack (alignment: .center){
TextField("0", text: $VolumenstromEingabe,
onCommit : { self.BerechungVG1(); self.BerechungVG2()} )
.textFieldStyle(RoundedBorderTextFieldStyle())
.modifier(ClearButton(text: $VolumenstromEingabe))
.frame(width: 100, height: 50)
.keyboardType(.numbersAndPunctuation)
.ignoresSafeArea(.keyboard)
TextField("0", text: $GeschwindigkeitEingabe,
onCommit: {self.BerechungVG1(); self.BerechungVG2()})
.textFieldStyle(RoundedBorderTextFieldStyle())
.modifier(ClearButton(text: $GeschwindigkeitEingabe))
.frame(width: 100, height: 50)
.keyboardType(.numbersAndPunctuation)
.ignoresSafeArea(.keyboard)
TextField("0", text: $KanalAEingabe,
onCommit: { self.BerechungVGA()})
.textFieldStyle(RoundedBorderTextFieldStyle())
.modifier(ClearButton(text: $KanalAEingabe))
.frame(width: 100, height: 50)
.keyboardType(.numbersAndPunctuation)
.ignoresSafeArea(.keyboard)
TextField("0", text: $KanalBEingabe,
onCommit: { self.BerechungVAB(); self.BerechungVGD()
})
.textFieldStyle(RoundedBorderTextFieldStyle())
.modifier(ClearButton(text: $KanalBEingabe))
.frame(width: 100, height: 50)
.keyboardType(.numbersAndPunctuation)
.ignoresSafeArea(.keyboard)
TextField("0", text: $KanalDErgebnis,
onCommit: { self.BerechungDg(); self.BerechungVG1()})
.textFieldStyle(RoundedBorderTextFieldStyle())
.modifier(ClearButton(text: $KanalDErgebnis))
.frame(width: 100, height: 50)
.keyboardType(.numbersAndPunctuation)
.ignoresSafeArea(.keyboard)
}
VStack (alignment: .leading, spacing: 8){
Text("in m³/h")
.font(.body)
.fontWeight(.bold)
.frame(height: 50)
.ignoresSafeArea(.keyboard)
Text("in m/s")
.font(.body)
.fontWeight(.bold)
.frame(height: 50)
.ignoresSafeArea(.keyboard)
Text("in mm")
.font(.body)
.fontWeight(.bold)
.frame(height: 50)
.ignoresSafeArea(.keyboard)
Text("in mm")
.font(.body)
.fontWeight(.bold)
.frame(height: 50)
.ignoresSafeArea(.keyboard)
Text("in mm")
.font(.body)
.fontWeight(.bold)
.frame(height: 50)
.ignoresSafeArea(.keyboard)
}
}
Button(action: {
self.ClearAll()
}) {
Text("Reset")}
Spacer()
HStack{
VStack{
Spacer()
Image("Logo Martin neu-60")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(height: 80)
.padding()
.ignoresSafeArea(.keyboard)
Button(action: {
self.showingInfoSheet.toggle()
}) {
Text("Informationen")
.ignoresSafeArea(.keyboard)
}
.sheet(isPresented: $showingInfoSheet, content: {
ScrollView1()
})
Text("Luftkanalrechner v1.0, Build 6")
.padding()
.ignoresSafeArea(.keyboard)
}
}
}

rounded rectangle as a background in swiftUI

I tried to do an overlay, but it doesn't work ,
That part on the bottom keeps bugging me.
is there any other method beside doing an overlay of a rectangle??
ZStack {
RoundedRectangle(cornerRadius: 15)
.foregroundColor(Color("Hello"))
.frame(height: 700, alignment: /*#START_MENU_TOKEN#*/.center/*#END_MENU_TOKEN#*/)
.overlay(
VStack {
RoundedRectangle(cornerRadius: 15)
.frame(width:381, height: 130.36, alignment: /*#START_MENU_TOKEN#*/.center/*#END_MENU_TOKEN#*/)
.shadow(radius: 4,y:3)
.foregroundColor(.white)
.overlay(
HStack {
Button(action: /*#START_MENU_TOKEN#*//*#PLACEHOLDER=Action#*/{}/*#END_MENU_TOKEN#*/) {
ButtonBig(ButtonText: "Keuangan",ButtonIcon: "creditcard.fill")
}
Button(action: /*#START_MENU_TOKEN#*//*#PLACEHOLDER=Action#*/{}/*#END_MENU_TOKEN#*/) {
ButtonBig(ButtonText: "Penjurusan",ButtonIcon: "figure.walk")
}
Button(action: /*#START_MENU_TOKEN#*//*#PLACEHOLDER=Action#*/{}/*#END_MENU_TOKEN#*/) {
ButtonBig(ButtonText: "Status",ButtonIcon: "person.crop.circle.badge.plus")
}
}.buttonStyle(PlainButtonStyle()))
RoundedRectangle(cornerRadius: 15)
.frame(width:381, height: 535, alignment: /*#START_MENU_TOKEN#*/.center/*#END_MENU_TOKEN#*/)
.shadow(radius: 4,y:3)
.foregroundColor(.white)
}
I wanted to create something like that basically
if there's a library for it, please do tell me. Thank you
Try these 2 modifiers:
.background(RoundedRectangle(cornerRadius: 10))
.clipped()

Resources