Build: Frame Modifier Demo
This screen shows a colored rectangle with a frame modifier applied to control its size and alignment.
Target UI
--------------------- | | | [ Blue Box ] | | | ---------------------
--------------------- | | | [ Blue Box ] | | | ---------------------
import SwiftUI struct FrameModifierDemo: View { var body: some View { VStack { // TODO: Add blue rectangle with frame modifier here // TODO: Add label below rectangle } .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.white) } } struct FrameModifierDemo_Previews: PreviewProvider { static var previews: some View { FrameModifierDemo() } }
import SwiftUI struct FrameModifierDemo: View { var body: some View { VStack(spacing: 20) { Rectangle() .fill(Color.blue) .frame(width: 150, height: 100) Text("Blue Box") .font(.headline) .foregroundColor(.black) } .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.white) .edgesIgnoringSafeArea(.all) } } struct FrameModifierDemo_Previews: PreviewProvider { static var previews: some View { FrameModifierDemo() } }
We use a Rectangle view and fill it with blue color. The frame modifier sets the rectangle's width to 150 points and height to 100 points. The VStack arranges the rectangle and the label vertically with some spacing. The whole stack is centered by setting its frame to take the full available space with maxWidth and maxHeight set to infinity. This centers the content both horizontally and vertically. The label below the rectangle uses a headline font and black color for good contrast.
--------------------- | | | | | █████████ | | █████████ | | █████████ | | | | Blue Box | | | ---------------------
frame modifier do in SwiftUI?Rectangle() .fill(Color.red) .frame(width: 150, height: 100) .frame(width: 200, height: 200)
Text("Hello")
.frame(width: 100 height: 50)Pass the test and this pattern moves unknown