Posts

Post not yet marked as solved
3 Replies
0 Views
I have added the myView.translatesAutoresizingMaskIntoConstraints = false code line to my code. import UIKit class ViewController: UIViewController {     var myView = UIView()     override func viewDidLoad() {         super.viewDidLoad()         myView.frame.size.width = 100         myView.frame.size.height = 100         myView.backgroundColor = .green         view.addSubview(myView) myView.translatesAutoresizingMaskIntoConstraints = false         let margins = view.layoutMarginsGuide         myView.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true     } } But now. i get a blank screen. Why is this happening?
Post marked as solved
4 Replies
0 Views
Thank you so much guys! I appreciate your help!
Post marked as solved
5 Replies
0 Views
Thank you very very much! It works! There are many good tutorials and sample codes written for the released version of SwiftUI. You should better find a good one unless you want to study how the beta version of SwiftUI was. Yes, you are right! I just want to go to the end in this tutorial)) And then go to the released version. And the second reason that the basics are well shown here, the main ideas of SwiftUI. When I see the general idea, it is easier for me to understand the specific methods. Thanks a lot again!
Post marked as solved
5 Replies
0 Views
ok, thanks for helping. Below is the complete project code for files. At the very end, there is a link to the full project in the archive. This is a RoomsApp.swift file code: import SwiftUI @main struct RoomsApp: App {     var body: some Scene {         WindowGroup {             ContentView()         }     } } This is ContentView.swift code: import SwiftUI struct ContentView: View {     @ObservedObject var store = RoomStore()     var body: some View {         NavigationView {             List {                 Button(action: addRoom) {                     Text("Add Room")                 }                 ForEach(store.rooms) { room in                     RoomCell(room: room)                 }             }             .navigationBarTitle(Text("Rooms"))         }     }     func addRoom() {         store.rooms.append(Room(name: "Hall 2", capacity: 2000))     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView(store: RoomStore(rooms: testData))     } } struct RoomCell: View {     var room: Room         var body: some View {         NavigationLink(destination: RoomDetail(room: room)) {                         Image(room.thumbnailName)                 .cornerRadius(8)             VStack(alignment: .leading) {                 Text(room.name)                 Text("\(room.capacity) people")                     .font(.subheadline)                     .foregroundColor(.secondary)             }         }     } } RoomDetail.swift file code: import SwiftUI struct RoomDetail: View {     var room: Room     @State private var zoomed = false     var body: some View {         ZStack(alignment: .topLeading) {             Image(room.imageName)                 .resizable()                 .aspectRatio(contentMode: zoomed ? .fill : .fit)                 .navigationBarTitle(room.name, displayMode: .inline )                 .onTapGesture { // Instead of TapAction                     withAnimation(.easeIn(duration: 1)) { zoomed.toggle() } // .easyIn is instead of basic(duration)                 }                 .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)             if room.hasVideo && !zoomed {                 Image(systemName: "video.fill")                     .font(.title)                     .padding(.all)                     .transition(.move(edge: .leading))             }         }     } } struct RoomDetail_Previews: PreviewProvider {     static var previews: some View {         Group {             NavigationView{RoomDetail(room: testData[0])}             NavigationView{RoomDetail(room: testData[1])}         }     } } Room import SwiftUI struct Room: Identifiable {     var id = UUID()     var name: String     var capacity: Int     var hasVideo: Bool = false     var imageName: String {         return name     }     var thumbnailName: String {         return name  + "Thumb"     } } let testData = [     Room(name: "room-1", capacity: 6, hasVideo: true),     Room(name: "room-2", capacity: 8, hasVideo: false),     Room(name: "room-3", capacity: 16, hasVideo: true),     Room(name: "room-4", capacity: 10, hasVideo: true),     Room(name: "room-5", capacity: 12, hasVideo: false),     Room(name: "room-6", capacity: 8, hasVideo: false),     Room(name: "room-7", capacity: 10, hasVideo: true),     Room(name: "room-8", capacity: 7, hasVideo: false),     Room(name: "room-9", capacity: 11, hasVideo: false) ] RoomStore import SwiftUI import Combine class RoomStore: ObservableObject { // Instead of BindableObject     var rooms: [Room] {         didSet { didChange.send() }     }     init(rooms: [Room] = []) {         self.rooms = rooms     }     var didChange = PassthroughSubject<Void, Never>() } This is a link to the complete project in the archive. https://drive.google.com/file/d/1Sis3r57S9n9Uf4Pd8dFSyMoM-Z0vsj9K/view?usp=sharing
Post marked as solved
5 Replies
0 Views
Yes, I understand that the language has changed, and I even tried to find these changes, but it did not work out. That's why I asked a question on this forum. Perhaps there is someone here who has done this tutorial and can say what has changed?
Post marked as solved
11 Replies
0 Views
Thanks a lot guys! I really appreciate your help!
Post marked as solved
11 Replies
0 Views
Уes, thanks for the help. Now I realized that the whole point is in this line of code: item.isSelected.toggle() Here, the value is not checked, here it is changed. Let me ask one last question on this topic: if foods were class, would that make a difference? In fact , a class is a reference type and if we write such code var item = foods[indexPath.row] then it turns out that we are passing the link and the above line should perform the change. item.isSelected.toggle() Is that right?
Post marked as solved
11 Replies
0 Views
You wrote: "Unless you call reloadRows(at:with:) or reloadData() (generally, reloadData() is too much just for updating a single cell), UITableView would not update the cell, meaning tableView(_:cellForRowAt:) would not be called." I understand that. My question is about something else - why is the same operation performed differently? Inside the cellForRowAt method, I can create a separate constant to hold the item from array and then check its isSelected property. let food = foods[indexPath.row] if food.isSelected {... But inside the didSelectRowAt method, this code does not work and I need to retrieve an item from the original array on the fly to check its isSelected property. if foods[indexPath].isSelected... What could be the reason here? Any ideas please
Post marked as solved
11 Replies
0 Views
P.S. But why is this happening? After all, next to it, in the cellForRowAt method, the same previous code is used: ... let food = foods[indexPath.row]         ...         // Assign checkmark         if food.isSelected {             cell.accessoryType = .checkmark         } else {             cell.accessoryType = .none         } ... And it works. Any ideas on this point, please.
Post marked as solved
11 Replies
0 Views
Thank you so much! I made a following code:     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {         // Create variable and check it.         guard let cell = tableView.cellForRow(at: indexPath) else { return }          //Clean cell format before select food         cell.accessoryType = .none         // Set checkmark         if foods[indexPath].isSelected == false {             cell.accessoryType = .checkmark             foods[indexPath].isSelected.toggle()         // Remove checkmark         } else {             cell.accessoryType = .none             foods[indexPath].isSelected.toggle()         } LoadSaveData.saveToFile(foods: foods)         tableView.reloadData()     } It works!
Post marked as solved
11 Replies
0 Views
"How do you deselect?" I use this if condition inside of didSelectRowAt method // Set a checkmark if item.isSelected == false { cell.accessoryType = .checkmark item.isSelected.toggle()// // Remove a checkmark } else { cell.accessoryType = .none item.isSelected.toggle()// } The fact is that in this condition only the first part is executed, and the second part (else) is never executed. And I cannot understand why? "call func reloadROws..." I call tableView.reloadData() but it doesn't work.
Post marked as solved
1 Replies
0 Views
Create your own small applications. For example, currency converter, to-do list, etc.
Post not yet marked as solved
2 Replies
0 Views
I think that this should be looked for in some new frameworks
Post marked as solved
10 Replies
0 Views
Maybe you need to switch the mode of displaying messages in the console. It is located at the bottom of the Playground. Look at the screenshot below
Post marked as solved
2 Replies
0 Views