How can I make the list wider in SwiftUI? - swift

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

Related

SwiftUI TabView Page Styling

I am using a tab view with PageTabViewStyle to list out colors of a shirt, but the tab bar is barely visible with lighter color shirts and only shows the top half on darker. Is it possible to give the bar a background or change the dot color?
I've tried:
init() {
UITabBar.appearance().barTintColor = .darkGray
}
But it didn't work.
Full Code:
struct PrintifyProductCell: View {
var product: PrintifyProduct
var images: [PrintifyImages] {
product.images.filter { $0.position == "front" }
}
#State private var animating = true
#State private var selected = 0
var body: some View {
VStack {
TabView(selection: $selected) {
ForEach(images.indices, id: \.self) { index in
AsyncImage(url: URL(string: images[index].src)!) { image in
image
.resizable()
.frame(minWidth: 120, idealWidth: 160, maxWidth: 160, minHeight: 130, idealHeight: 180, maxHeight: 180, alignment: .center)
.cornerRadius(8)
.tag(index)
} placeholder: {
LoadingView(isAnimating: $animating)
.frame(width: 40, height: 40)
}
}
.frame(minWidth: 120, idealWidth: 160, maxWidth: 160, minHeight: 130, idealHeight: 180, maxHeight: 180, alignment: .center)
}
.tabViewStyle(PageTabViewStyle())
.frame(minWidth: 120, idealWidth: 160, maxWidth: 160, minHeight: 130, idealHeight: 180, maxHeight: 180, alignment: .center)
.cornerRadius(8)
.padding(.horizontal, 6)
.padding(.top, 6)
Text(product.title)
}
.background(Color(UIColor.uStadium.lightGray))
.cornerRadius(8)
}
}

Fit Image & Text in Frame SwiftUI

I have two problems with my code.
I want to fit my Image into the frame I created. The image format is not the same with that of the frame, I would rather have just part of the image there instead of white borders.
The title text I created with the blur background should fit into the bottom of the frame.
struct VisualEffectView: UIViewRepresentable {
var effect: UIVisualEffect?
func makeUIView(context: UIViewRepresentableContext<Self>) -> UIVisualEffectView { UIVisualEffectView() }
func updateUIView(_ uiView: UIVisualEffectView, context: UIViewRepresentableContext<Self>) { uiView.effect = effect }
}
struct ContentView: View {
var body: some View {
ZStack {
Color.black.edgesIgnoringSafeArea(.all)
ZStack {
Image("Yosemite")
.resizable()
.scaledToFill()
.overlay(content: {
Text("Lorem ipsum dolor sit amet")
.font(.largeTitle)
.fontWeight(.bold)
.multilineTextAlignment(.leading)
.fixedSize(horizontal: false, vertical: true)
.foregroundColor(.white)
.background(VisualEffectView(effect: UIBlurEffect(style: .systemMaterialDark)))
.frame(minWidth: 350, maxWidth: .infinity, minHeight: 250, maxHeight: .infinity, alignment: .bottom)
.scaledToFit()
})
}.padding()
.frame(width: 350, height: 400)
.background()
.clipShape(RoundedRectangle(cornerRadius: 30))
}
}
}
I don't know about your blur background, but the rest would work like this:
struct ContentView: View {
var body: some View {
ZStack {
Color.black.edgesIgnoringSafeArea(.all)
Image("image")
.resizable()
.scaledToFill()
.frame(width: 350, height: 400)
.cornerRadius(30)
.overlay {
Text("Lorem ipsum dolor sit amet")
.font(.largeTitle)
.fontWeight(.bold)
.multilineTextAlignment(.leading)
.foregroundColor(.white)
.padding(.horizontal, 8)
.background(.ultraThinMaterial)
.frame(maxHeight: .infinity, alignment: .bottom)
.padding()
}
}
}
}

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