Soy nuevo en Swift y estoy creando una aplicación Buscaminas para iOS. En la parte superior de la pantalla, quiero tener una imagen de una bomba junto a un objeto Text() que muestre la cantidad de bombas restantes.
Me gustaría que la imagen y/o HStack cambien de tamaño a la altura del texto adyacente sin tener que codificar sus dimensiones.
¿Se puede/cómo se puede lograr esto?
El código hasta ahora se ve así:
HStack(alignment: .center, spacing: 10) { Image("top_bomb") .resizable() .aspectRatio(contentMode: .fit) Text(String(game.bombsRemaining)) .font(.system(size: 30, weight: .bold, design: .monospaced)) } .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 0.1, alignment: .trailing)@State var labelHeight = CGFloat.zero HStack(alignment: .center, spacing: 10) { Image("top_bomb") .resizable() .aspectRatio(contentMode: .fit) .frame(maxHeight: labelHeight) Text(String(game.bombsRemaining)) .font(.system(size: 30, weight: .bold, design: .monospaced)) .overlay( GeometryReader(content: { geometry in Color.clear .onAppear(perform: { self.labelHeight = geometry.frame(in: .local).size.height }) }) ) } .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height * 0.2, alignment: .trailing)Solo necesita arreglarlo por tamaño, como
HStack(alignment: .center, spacing: 10) { Image("top_bomb") .resizable() .aspectRatio(contentMode: .fit) Text(String(game.bombsRemaining)) .font(.system(size: 30, weight: .bold, design: .monospaced)) } .fixedSize() // << here !! .frame(maxWidth: .infinity, alignment: .trailing) // << !!Nota: también preste atención a que no necesita codificar el marco al tamaño de la pantalla