I a having an issue with my button "Get New Connection Pin" It seem to be frozen or stuck at times. It's as it's not responding to being pressed. I have to keep pressing the button a few times before it will actually response. Some times its works and some times it does not. I am new to SwiftUI and not sure what I am doing wrong.
I notice that when I remove the styling from the button text and the button everything works great as I intended however when I add the styling I have issue with it responding
//
// MainView.swift
// Remote Bridge
//
// Created by Jerry Seigle on 6/19/23.
//
import SwiftUI
struct MainView: View {
// TODO: our app targets macOS 10.15. Update to @StateObject when targeting macOS 12 or later
@ObservedObject private var viewModel = MainViewModel()
@ObservedObject private var accountViewModel = AccountTabViewModel()
var body: some View {
VStack(spacing: 20) {
Spacer()
VStack(alignment: .leading, spacing: 10) {
Text("Connection Pin:")
.font(.headline)
.foregroundColor(.gray)
Button(action: {
viewModel.copyToClipboard(text: viewModel.roomIdString)
}) {
HStack {
Text(viewModel.roomIdString)
.fontWeight(.semibold)
.frame(maxWidth: .infinity, alignment: .leading)
Image("doc.on.doc.fill")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 16, height: 16)
.foregroundColor(.blue)
}
}
.cornerRadius(8)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding()
.background(Color.gray.opacity(0.2))
.cornerRadius(8)
Button(action: viewModel.getRoomId) {
Text("Get New Connection Pin")
.frame(maxWidth: .infinity, minHeight: 40)
.background(Color.blue)
.cornerRadius(4)
.contentShape(Rectangle()) // Make the entire button tappable
}
.buttonStyle(PlainButtonStyle())
if !accountViewModel.isSubscriptionActive {
Button(action: viewModel.upgradeForProAccessTap) {
Text("Upgrade for Pro Access!")
.font(.system(size: 14, weight: .regular, design: .default).italic())
.foregroundColor(.orange)
}
.frame(maxWidth: .infinity, alignment: .leading)
.buttonStyle(PlainButtonStyle())
}
Spacer()
}
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.onAppear(perform: viewModel.loadSavedRoomId)
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
.frame(width: 225, height: 180)
.background(Color.white.opacity(0.1))
}
}
I am referring to this button
Button(action: viewModel.getRoomId) {
Text("Get New Connection Pin")
.frame(maxWidth: .infinity, minHeight: 40)
.background(Color.blue)
.cornerRadius(4)
.contentShape(Rectangle()) // Make the entire button tappable
}
.buttonStyle(PlainButtonStyle())
it seems as it start to get stuck when I add
.buttonStyle(PlainButtonStyle())