Ok I found a solution @darkpaw, with .overlay(). But I don't know if it's the best solution. Maybe there is a more optimized or "normal" way to do it. Here is the code and rendering:
import SwiftUI
struct DashboardView: View {
var body: some View {
HStack {
VStack {
RoundedRectangle(cornerRadius: 25.0)
.fill(Color.red)
.overlay(Text("Text 1").foregroundColor(.white))
RoundedRectangle(cornerRadius: 25.0)
.fill(Color.green)
.overlay(Text("Text 2").foregroundColor(.white))
}
VStack {
RoundedRectangle(cornerRadius: 25.0)
.fill(Color.blue)
.overlay{
VStack{
Spacer()
Text("Text 3")
.foregroundColor(.white)
Spacer()
Text("Text 4").foregroundColor(.white)
Spacer()
}
}
}
}
.frame(height: 200)
.padding()
}
}
#Preview {
DashboardView()
}
Post
Replies
Boosts
Views
Activity
In fact, I've spoken too soon @darkpaw : the layout of the rectangles is exactly what I want, but when I try to add text or elements to the rectangles, their layout changes completely. Here's my code and the rendering:
import SwiftUI
struct DashboardView: View {
var body: some View {
HStack {
VStack {
Text("Hello, World!")
.background(RoundedRectangle(cornerRadius: 25.0).fill(Color.red))
Text("Hello, World!")
.background(RoundedRectangle(cornerRadius: 25.0).fill(Color.red))
}
VStack {
Text("Hello, World!")
.background(RoundedRectangle(cornerRadius: 25.0).fill(Color.red))
}
}
.frame(height: 200)
.padding()
}
}
#Preview {
DashboardView()
}
Hi, for it's not working, your app crash before the end of the process ... What can I do ? (Tell me if u need more info about it, and about my project configuration)
@Claude31 so adapted your code to my project and I tested. Well done it's working. Thanks a lot. I am not familiar with such binding variables. Thanks for helping me out.
@Claude31 in fact it can might works. I will try
Hey thanks for answering @Claude31 but that does not fit with I what I want. There is the all code of the page and a brief resumé of the idea. It's and AddView called AddExhibView to implement new element in a Database, so I want after pressing the save button to show the ListView called ExhibitView where I display all the data. There is the code :
import SwiftUI
import CoreData
struct AddExhibView: View {
@Environment(\.dismiss) var dismiss
@Environment(\.managedObjectContext) private var viewContext
@State private var name: String = ""
@State private var info: String = ""
@State private var isOnline: Bool = false
@State private var startDate: Date = Date()
@State private var endDate: Date = Date()
@State private var contactBuyer = false
@State private var showAlert: Bool = false
@State private var alertMessage: String = "Il en manque ..."
//MARK: Drag&Drop Stuff
@State var firstImage = NSImage(named: "image")
@State var firstUrl: URL?
@State var secondImage = NSImage(named: "image")
@State var secondUrl: URL?
@State var thirdImage = NSImage(named: "image")
@State var thirdUrl: URL?
//MARK: Aheviement Stuff
@State private var showAchievement: Bool = false
let achievement = Achievement(title: "Succès", description: "L'élément a été ajouté avec succès")
@State private var showExhibitView: Bool = false
var body: some View {
HStack{
DropImage(image: $firstImage, url: $firstUrl)
if firstImage != nil {
DropImage(image: $secondImage, url: $secondUrl)
if secondImage != nil{
DropImage(image: $thirdImage, url: $thirdUrl)
}
}
}
Form{
VStack{
TextField("Titre", text: $name)
.textFieldStyle(.roundedBorder)
HStack{
DatePicker("Début", selection: $startDate)
DatePicker("Fin", selection: $endDate)
Spacer()
Toggle("Informer les acheteurs", isOn: $contactBuyer)
Toggle("Mettre en ligne", isOn: $isOnline)
}
HStack{
VStack{
Text("Description")
Spacer()
}
TextEditor(text:$info)
.textFieldStyle(.roundedBorder)
}
}
}
.padding()
//MARK: Save
HStack{
Button("Validez"){
print("validez")
checkCondition()
if !showAlert{
addToBase()
showAchievement = true
Timer.scheduledTimer(withTimeInterval: 1.01, repeats: false) { _ in
showExhibitView = true
}
}
}
.alert(isPresented: $showAlert){
Alert(title: Text("Oops ?"),
message: Text(alertMessage))
}
.sheet(isPresented: $showAchievement) {
AchievementPopView(achievement: achievement, isPresented: $showAchievement)
}
}
.padding()
if showExhibitView{
ExhibitView()
}
}
private func checkCondition(){
if name == "" || firstImage == nil {
showAlert = true
return
}
}
private func addToBase(){
let firstImageData = jpegDataFrom(image: firstImage!)
if secondImage != nil && thirdImage == nil{
let secondImageData = jpegDataFrom(image: secondImage!)
DataController().addExhibit(name: name,
info: info,
firstImage: firstImageData,
secondImage: secondImageData,
thirdImage:nil,
startDate: startDate,
endDate: endDate,
isOnline: isOnline,
contactBuyer: contactBuyer,
context:viewContext)
print("2")
return
}
if secondImage != nil && thirdImage != nil {
let secondImageData = jpegDataFrom(image: secondImage!)
let thirdImageData = jpegDataFrom(image: thirdImage!)
DataController().addExhibit(name: name,
info: info,
firstImage: firstImageData,
secondImage: secondImageData,
thirdImage:thirdImageData,
startDate: startDate,
endDate: endDate,
isOnline: isOnline,
contactBuyer: contactBuyer,
context:viewContext)
print("3")
return
}
DataController().addExhibit(name: name,
info: info,
firstImage: firstImageData,
secondImage: nil,
thirdImage:nil,
startDate: startDate,
endDate: endDate,
isOnline: isOnline,
contactBuyer: contactBuyer,
context:viewContext)
print("1")
return
}
}
Thank for your response @newwbee.
I figure out the problem ! I just update my Mac to the latest Beta version of Sonoma. Restart Xcode and run the project and everything work well !
it still dos not work. any news ?