How to set width and height of an image in SwiftUI? - swift

I am trying to set height and width of an image . I've tried using following code but it didn't work.
Image("testImg")
.frame(width: 50, height: 50)

Try this
Image("testImg")
.resizable()
.frame(width: 50.0, height: 50.0)

Image(restaurant.image)
.resizable()
.frame(width: 40, height: 40)
.clipShape(Circle())

Try this code:
Image("testImg")
.resizable()
.scaledToFit()
.frame(width: 50,height:50)

Image("testImg")
.resizable()
.scaledToFit()
.frame(width: required-width,height:required-height)

var body: some View {
Image("yourImage")
.resizable()
.scaledToFit()
.frame(width: 50.0, height:50.0)
}

Related

How do I draw a shape with multiple borders?

I am trying to draw a shape using swift, and here my code
struct ElementView: View {
var element: Element
var body: some View {
let rec = RoundedRectangle(cornerRadius: 25.0)
return ZStack {
rec
.frame(width: 300, height: 150, alignment: .center)
.foregroundColor(.pink)
.opacity(0.4)
.overlay(
rec.stroke(
Color.pink,
style: StrokeStyle(
lineWidth: 5,
lineCap: .round,
lineJoin: .round
)
)
)
Text("Text")
}
}
}
There is only one border, but I want two or more
My result: https://i.stack.imgur.com/n26Vb.png
What is expected: https://i.stack.imgur.com/L23cW.png
How can I add multiple borders?
Another approach with less code!
struct ElementView: View {
var body: some View {
Text("Text")
.frame(width: 300, height: 150, alignment: .center)
.background(Color.yellow.opacity(0.4))
.cornerRadius(15.0)
.overlay(RoundedRectangle(cornerRadius: 15.0).strokeBorder(Color.blue, lineWidth: 10.0))
.padding(10.0)
.overlay(RoundedRectangle(cornerRadius: 25.0).strokeBorder(Color.red, lineWidth: 10.0))
}
}

How can I add fixed-width space to beginning of HStack?

In SwiftUI I am adding buttons to an HStack. I want a space of a fixed width at the beginning of the HStack. Currently the buttons are directly up against the left edge of the screen and I want a space of 64px before the first button. I cannot seem to find an answer to this, my Google-Fu may be failing me.
Here is what I have:
struct MyView: View {
var body: some View {
HStack(alignment: .center, spacing: 12, content: {
Button(action: doThis) {
Image("this").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
Button(action: doThat) {
Image("that").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
Spacer()
})
}
func doThis() {}
func doThat() {}
}
Just add some padding on the left.
struct ContentView: View {
var body: some View {
HStack(alignment: .center, spacing: 12, content: {
Button(action: doThis) {
Image("this").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
.padding(.leading, 64) /// here!
Button(action: doThat) {
Image("that").resizable().aspectRatio(contentMode: .fit)
}.frame(width: 38, height: 38, alignment: .center)
Spacer()
})
}
func doThis() {}
func doThat() {}
}
Before
After

How can I add a button in SwiftUI

How can I add a button at this location in SwiftUI? I have a textfield and a background being used, but I'm not sure how you can add a button to this location, like a bar button in swift.
My code:
struct MyTextField: View
{
#ObservedObject var model: TextFieldModel
var body: some View
{
VStack(spacing: 0)
{
Color.black
.frame(height: 100)
.overlay(Image(("appIconLogo")).resizable().frame(width: 70, height: 70),
alignment: .bottom)
.edgesIgnoringSafeArea(.top)
VStack(alignment: .leading, spacing: 10)
{
Text("Welcome!")
.font(.system(size: 60, design: .rounded))
.foregroundColor(.black)
.padding(.leading)
iTextField("Search Your Ticker", text: $model.text, isEditing: $model.isEditing)
.foregroundColor(.black)
.showsClearButton(true)
.autocapitalization(.allCharacters)
.disableAutocorrection(true)
.returnKeyType(.search)
.onReturn
{
print($model.text)
NotificationCenter.default.post(name: Notification.Name(rawValue: "isValidTicker"), object: nil)
}
.foregroundColor(.black)
.style(height: 58, font: nil, paddingLeading: 25, cornerRadius: 6, hasShadow: true, image: Image(systemName: "magnifyingglass"))
.foregroundColor(.black)
}.frame(maxWidth: .infinity, alignment: .leading)
}.frame(maxHeight: .infinity, alignment: .top)
}
}
I assume you wanted something like this
Color.black
.frame(height: 100)
.overlay(Image(("appIconLogo")).resizable().frame(width: 70, height: 70), alignment: .bottom)
.overlay(Button("Perform") {}.padding(), alignment: .leading) // << here!!
.edgesIgnoringSafeArea(.top)
Any particular reason you have the Color.black with an overlay for the logo? Using your code, wrapping it in a ZStack should work
ZStack(alignment: .leading) {
Color.black
.frame(height: 100)
.overlay(Image(("appIconLogo")).resizable().frame(width: 70, height: 70), alignment: .bottom)
.edgesIgnoringSafeArea(.top)
Button(action:{}) {
Text("Button Text")
}
}

How to: Create buttons Swift UI

I have these frames positioned on my view, exactly how I need buttons placed but am new to swift - UI and trying to figure out how to do this the right way. Can I somehow bring the button onto the stack above the background color of the VStack? Also is VStack the correct way to approach this?
Code for reference :
//Todays List
VStack(alignment: .leading) {
Color.black
.frame(width: 205, height: 70, alignment: .center)
.position(x: 80, y: 80)
}
//Tomorrows List
VStack(alignment: .leading) {
Color.black
.frame(width: 205, height: 70, alignment: .center)
.position(x: 290, y: 80)
}
//This Month
VStack(alignment: .leading) {
Color.blue
.frame(width: 205, height: 70, alignment: .center)
.position(x: 80, y: 150)
}
//Next Month
VStack(alignment: .leading) {
Color.blue
.frame(width: 205, height: 70, alignment: .center)
.position(x: 290, y: 150)
}
//3% Yeild Or Higher
VStack(alignment: .leading) {
Color.red
.frame(width: 205, height: 70, alignment: .center)
.position(x: 80, y: 215)
}
//5% Yield Or Higher
VStack(alignment: .leading) {
Color.red
.frame(width: 205, height: 70, alignment: .center)
.position(x: 290, y: 215)
}
//App Help
VStack(alignment: .leading) {
Color.green
.frame(width: 205, height: 70, alignment: .center)
.position(x: 80, y: 285)
}
//Exit App / TBD
VStack(alignment: .leading) {
Color.yellow
.frame(width: 205, height: 70, alignment: .center)
.position(x: 290, y: 285)
}
}
[1]: https://i.stack.imgur.com/Fe64N.png
To make a button is swift ui you use the button command and then an action like this
VStack { Button(action: {//Code for you button to do something)}) {
Text("LOGIN")
//you can add text to the button by just adding text in the curly brackets and add modifiers to id such as .padding() or .cornerRadius
}
To add a button in you VStacks you do this
VStack(alignment: .leading) {
Button(action: {
// Write your action here
}) {
Color.blue
}
.frame(width: 205, height: 70, alignment: .center)
.position(x: 80, y: 150)
}
But that view you have really need some work, you shouldn't hardcode the position of each stack and add the frame to each of them, if it's possible for you to use SWiftUI 2.0 read something about using LazyVGrid https://developer.apple.com/documentation/swiftui/lazyvgrid

How can I make the list wider in SwiftUI?

I'm learning SwiftUI right now and I'm working on a tutorial.
But in SwiftUI List, the part of name is cut like the picture. Do you have a modifier?
struct ContentView: View {
var body: some View {
List(modelData) {
Device in HStack {
Image(systemName: Device.image)
.frame(width: 50, height: 10, alignment: .leading)
Text("\(Device.name)")
.frame(width: 50, height: 10, alignment: .leading)
.scaledToFit()
VStack {
Text("\(Device.price)")
}
}
.font(.title)
}
}
}
Instead of limiting as you do
Text("\(Device.name)")
.frame(width: 50, height: 10, alignment: .leading)
.scaledToFit()
use just
Text("\(Device.name)")
It will be automatically sized to fit