How do I make an image show on the namecard on playgrounds - swift

enter image description here
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Image("MyImage")
.resizable()
.scaledToFit()
Text("I am Jia Ying")
}
}
}
I am creating a name card for an app project. I typed in the above code, selected a photo from the photo gallery as the app icon and ran the app. I was successful once, however as I tried it again, only the words showed up. I looked at the image asset and found it was horizontal (the initial photo was vertical). Could the horizontal image asset be related to the bug? Can anyone suggest a solution to fix the problem? I greatly appreciate it!

Related

Why does the MacMenuBarExtra label only work for sf symbol images and text?

I'm using a MacMenuBarExtra for my macOS app, however, I can't seem to make any view that's not a sf symbol image or text render properly. Here is the code I am using in my app file and if you run it, you'll see that no icon shows in the menu bar. I want to create a dynamic icon which will show up there, does anyone have any idea why this is happening?
MenuBarExtra {
ContentView()
} label: {
Circle().foregroundColor(.green)
}
.menuBarExtraStyle(.window)

Set a background color for a view using .background()

I'm trying to follow Apple's introductory tutorial to SwiftUI.
In Section 3 Step 5 of the lesson, I get to the point where I have the following code:
import SwiftUI
struct CardView: View {
let scrum: DailyScrum
var body: some View {
Text(scrum.title) // returns the word "Design"
.font(.headline)
}
}
struct CardView_Previews: PreviewProvider {
static var scrum = DailyScrum.sampleData[0]
static var previews: some View {
CardView(scrum: scrum)
.background(scrum.theme.mainColor) // returns Color("yellow")
.previewLayout(.fixed(width: 400, height: 60))
}
}
According to the preview provided by the website, I should be getting the word "Design" with a yellow background, however, the yellow background doesn't show.
I tried changing the parameter to Color.yellow and it worked.
My question is, is there a difference between .background(Color("yellow")
and .background(Color.yellow), and if so why does apple's tutorial use the first one?
Yes. SwiftUI is relatively new technology, and is rapidly changing. Some changes was introduced into iOS 16 changing the .background view modifier... if you change your target to iOS 13 or iOS 14 .background(Color.yellow) should work. As a rule of thumb assume that the Apple's website docs and examples will always be a little bit old. So to use those websites they recommend that you download the example code, this code should be already set to older targets. But if you create a brand new project of your own and try to follow bit by bit some strange behavior could occur. The best documentation is the one embedded into the Xcode itself. Or, use the website for beginner learning using their example code.

Images not rendering in new iOS 16 Widgets?

I'm working on a new .accessoryInline widget, introduces with iOS 16. It's very simple, only displays static text and an image, said image is a custom SF Symbol, validated and also used in other places in which it works just fine (for example a .accessoryRectangular widget). Here is my code:
case .accessoryInline:
HStack {
Image("mysymbol.car")
.resizable()
.widgetAccentable()
Text("My Static Title")
}
This however only renders the text when running the app and leaves a weird small space where the image should be. Am I doing something wrong or is it a bug?
I'm facing this issue as well. Apparently there are size limits for inline widget icons. Resizable doesn't work, so the file itself has a size limit. There is no documentation on this anywhere, so I do not have the numbers yet.
Update: Using a PNG of size 145x145 seems to do the trick.
You will have to use Label to render image with text. HStack will not work there.
Label {
Text("My Static Title")
.foregroundColor(.white)
} icon: {
Image("mysymbol.car")
.resizable()
.frame(width: 12, height: 21, alignment: .center)
.foregroundColor(.white)
}
Draw your symbol on a .png-file, include that in your widget's bundle, and load the image through this .png-file.
I can assure you that loading a .png-file does work for me. There might be another issue that it doesn't work for you as you mentioned in a comment to #swiftyboi's answer.
.inlineAccessory is selective on images. I found that it does load SFSymbol images (through Image(systemName: String)) as well as bundle images in some but not all formats. However, it ignores images drawn with Path, so it does not surprise me that it also ignores custom symbols.
It also seems to ignore all formatting information. It just uses the first image and the first text it finds and displays them in this order.
Answer: What fixed it for me was moving the lockscreen widgets code from its own target to a shared target with the homescreen widgets and used WidgetBundle

Unable to see preview and unable to see the main window of my app (when running) while using swift ui for macOS developement

So I created a macOS cocoa project called "test" using swift ui in Xcode 11 beta and got problems at the very beginning.
Sorry in advance if my question is dumb.
The first problem is that the automatic preview says it is in pause. If I click resume, I got an error.
So I tried to run the code Xcode generated (I changed nothing in the code.) and it build successfully. When I tried to run the code freshly build, I come across the following error: EXC_BAD_INSTRUCTION
The line highlighted in red is the .frame line in the following piece of code:
import SwiftUI
struct ContentView : View {
var body: some View {
Text("Hello World")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
I'm on macOS Catalina Public Beta 1 so this is not a problem with the version of the system....
So, did I miss something? The tutorials I found online (I mostly looked on Youtube and Apple Dev websites) either show examples for iOS or show examples that are not starting from scratch ( For example on Apple websites, when they are demo-ing codes for macOS apps ).
Thanks in advance
EDIT
I'm adding this picture, it might help:
Actual screen when the app is running with the info of the threads section
Final answer:
Just needed an update Xcode to beta 2 :) .

Swift pages: Load remote image

I'm trying to load a remote image with the following code:
//: ![Xcode icon](http://devimages.apple.com.edgekey.net/assets/elements/icons/128x128/xcode.png "Some hover text")
But when I switch to "Show rendered markup" I only see the "Xcode icon" text.
Any of you knows why the swift pages are not showing the image? or what do I need to do for the swift pages to show the images?
I'll really appreciate your help.
I believe playgrounds will not load remote image for you, only quickhelp using that format will..
To load an image download png you want to show, add it to your playground resources and load using:
//: ![Xcode icon](xcode.png)