please help me to set image in macOs application using swiftUI
Here is my Implementation:
import SwiftUI
struct SigninView: View {
var body: some View {
VStack{
Image("Profile")
.resizable()
.foregroundColor(.white)
}
}
}
but image is not visible in View
import SwiftUI
struct SigninView: View {
var body: some View {
VStack{
Image("Profile") // this is image name from xcassets
// Image(nsImage: NSImage(named: NSImage.folderName) ?? NSImage()) // method for system image
.resizable()
.scaledToFill()
.frame(width: 75, height: 75)
.clipped()
.aspectRatio(contentMode: .fill)
.padding()
}
}
}
Related
hello i have an issue with my project on Xcode when I use navigation view, the window display not normally and appear on the right of the window already displayed,
here is the code.
NavigationLink(destination: Acceuil())
{
HStack{
Image("icone_connexion")
.font(.system(size: 15))
// .scaledToFill()
Text("se connecter")
.font(.system(size: 30))
}.cornerRadius(60)
.frame(width: 400, height: 60)
} .background(Capsule().fill(Color(red: 55/255, green: 66/255, blue: 114/255, opacity:1)))
.frame(width: 400, height: 60) //fin navigationlink
.buttonStyle(PlainButtonStyle())
I would like that the new window replace the older one:)
On macOS it is the standard behavior in NavigationView to show the views (parent view and destination view) next to each other: https://developer.apple.com/design/human-interface-guidelines/macos/windows-and-views/column-views/
If you don't want that you can do:
NavigationView {
...
}
.navigationViewStyle(.stack)
so I found this code
import SwiftUI
struct ContentView: View {
#State private var show = false
var body: some View {
VStack{
if !show {
RootView(show: $show)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.blue)
.transition(AnyTransition.move(edge: .leading)).animation(.default)
}
if show {
NextView(show: $show)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.green)
.transition(AnyTransition.move(edge: .trailing)).animation(.default)
}
}
}
}
struct RootView: View {
#Binding var show: Bool
var body: some View {
VStack{
Button("Next") { self.show = true }
Text("This is the first view")
}
}
}
it work grate for me so thanks you for your help
How can I add a circular view similar to the attachment below. In the attachment, the check is the image icon and I want to add the green background color in circular shape. I have a solution in Swift but, couldn't implement the same in swiftUI.
Related posts to my question: Add a border with cornerRadius to an Image in SwiftUI Xcode beta 5. But, this doesn't solve my issue.
Swift code to this implemention:
var imageView = UIImageView()
override init(theme: Theme) {
super.init(theme: theme)
imageView.clipsToBounds = true
setLayout()
}
override func layoutSubviews() {
super.layoutSubviews()
let cornerRadius = frame.height / 2
imageView.setCornerRadius(cornerRadius)
setCornerRadius(cornerRadius)
}
You could create this image like...
Image(systemName: "checkmark")
.resizable()
.frame(width: 20, height: 20)
.foregroundColor(.white)
.padding(20)
.background(Color.green)
.clipShape(Circle())
Or alternatively...
Image(systemName: "checkmark.circle.fill")
.resizable()
.frame(width: 40, height: 40) // put your sizes here
.foregroundColor(.green)
This is not the simplest thing to come up with. Use this struct as a separate view. It will return the image properly sized on the circle.
struct ImageOnCircle: View {
let icon: String
let radius: CGFloat
let circleColor: Color
let imageColor: Color // Remove this for an image in your assets folder.
var squareSide: CGFloat {
2.0.squareRoot() * radius
}
var body: some View {
ZStack {
Circle()
.fill(circleColor)
.frame(width: radius * 2, height: radius * 2)
// Use this implementation for an SF Symbol
Image(systemName: icon)
.resizable()
.aspectRatio(1.0, contentMode: .fit)
.frame(width: squareSide, height: squareSide)
.foregroundColor(imageColor)
// Use this implementation for an image in your assets folder.
// Image(icon)
// .resizable()
// .aspectRatio(1.0, contentMode: .fit)
// .frame(width: squareSide, height: squareSide)
}
}
}
I recently updated to Xcode 13 and I'm facing an issue with one image in my App
This is the same view with the same code in the same device but different iOS versions:
The image I'm using is this (Is a screenshoot instead of original image because original is over 2MB max limit image size of Stack Overflow):
The code:
struct HomeView: View {
#State private var navigateTo: Int? = Links.NIL
// Navigation in root and accent color change.
#Binding var rootViewLinks: RootView.Links
#State var navigationAccentColor: Color = Color(R.color.d_primary()!)
var body: some View {
NavigationView{
VStack{
//Logo and title
HStack{
Image(R.image.ic_logo_miloto_white.name)
.resizable()
.frame(width: 48, height: 56, alignment: .center)
.aspectRatio(contentMode: .fill)
Text("miloto")
.fontWeight(.bold)
.foregroundColor(.white)
.font(.system(size: 48))
.labelStyle(CustomLabelStyle())
}
Spacer()
//Welcome text
Text(R.string.localizable.homeSlogan())
.fontWeight(.bold)
.labelStyle(CustomLabelStyle())
.foregroundColor(.white)
.padding()
//Action buttons
HStack{
//ACCESS NAVIGATION
NavigationLink(
destination: AccessView(rootViewLinks: self.$rootViewLinks, navigationAccentColor: self.$navigationAccentColor),
tag: Links.ACCESS,
selection: $navigateTo){
Button(R.string.localizable.login_text()){
navigateTo = Links.ACCESS
}
.buttonStyle(PrimaryInvertedButtonStyle_W())
}
//REGISTER NAVIGATION
NavigationLink(
destination: RegisterView(rootViewLinks: self.$rootViewLinks, navigationAccentColor: self.$navigationAccentColor),
tag: Links.REGISTER,
selection: $navigateTo){
Button(R.string.localizable.register_text()){
navigateTo = Links.REGISTER
}
.buttonStyle(PrimaryButtonStyle_W())
}
}.padding(.horizontal)
//Tour text
NavigationLink(
destination: TourView(pages: TourPage.getHomeTutorialPages()),
tag: Links.TOUR,
selection: $navigateTo
) {
HStack {
Text(R.string.localizable.tour_text())
.fontWeight(.bold)
.labelStyle(CustomLabelStyle())
.foregroundColor(.white)
Image(R.image.ic_plane.name)
}
.padding(.vertical)
}
}
.background(
Image(R.image.bg_home_big.name)
.resizable()
.aspectRatio(nil, contentMode: .fill)
.ignoresSafeArea()
)
}
.accentColor(navigationAccentColor)
}
But you should focus on this:
.background(
Image(R.image.bg_home_big.name)
.resizable()
.aspectRatio(nil, contentMode: .fill)
.ignoresSafeArea()
)
**Why is image fill not working on iOS 15? Is this a bug? **
I want to have a full screen image in the background. I have implemented this:
struct LoginView: View {
var body: some View {
VStack {
Spacer();
Text("Hallo");
Text("Hallo2");
Text("Hallo2");
Text("Hallo2");
Text("Hallo2");
Text("Hallo2");
Text("Hallo2");
Spacer();
}.background(Image("Background LaunchScreen")
.resizable()
.aspectRatio(UIImage(named: "Background LaunchScreen")!.size, contentMode: .fill)
.clipped())
.edgesIgnoringSafeArea(.all)
}
}
When I remove the spacers, the image is no longer displayed in full screen mode.
Surely that can be solved differently?
And if I turn the iPhone in the simulator to the side, I have left and right white stripes.
How can I change this?
Here's a possible solution using GeometryReader and ZStack:
import SwiftUI
struct LoginView: View {
var body: some View {
GeometryReader { geometry in
ZStack {
Image("LaunchImage")
.resizable()
.aspectRatio(geometry.size, contentMode: .fill)
.edgesIgnoringSafeArea(.all)
VStack {
ForEach (1...10, id: \.self) { _ in
Text("Hallo")
.foregroundColor(Color.white)
}
}
}
}
}
}
#if DEBUG
struct LoginView_Previews: PreviewProvider {
static var previews: some View {
LoginView()
}
}
#endif
Results
I have created a round button with following code:
struct RoundButton : View {
var value = "..."
var body: some View {
GeometryReader { geometry in
Button(action: {}) {
VStack {
Text(self.value)
.font(.headline)
.color(Color("NightlyDark"))
Text("MIN")
.font(.footnote)
.color(.white)
}
.frame(width: geometry.size.width, height: geometry.size.height)
}
.clipShape(Circle())
}
}
}
But when clicking the button, the shape gets deformed. Any idea why this happens ?
Same happens when i use .mask(Circle())
Is this a beta thing or normal behavior ? And does anyone maybe know a better way to create rounded buttons ?
what happens here is Button Considers all screen[width + height] as their frame by default.
so you have to set Button also.
I think it's the default behavior in watchOS
Here Your Code :
struct RoundButton: View {
var value = "..."
var body: some View {
GeometryReader { geometry in
Button(action: {}) {
VStack {
Text(self.value)
.font(.headline)
.foregroundColor(Color("NightlyDark"))
Text("MIN")
.font(.footnote)
.foregroundColor(.white)
}
.frame(width: geometry.size.width, height: geometry.size.height)
}
.frame(width: geometry.size.width, height: geometry.size.height)
.clipShape(Circle())
}
}
}
Note: i'm using Xcode 11 Beta 4