I'm beginning to try out SwiftUI
and I'm surprised that it doesn't seem to be straightforward to change the background color of a View
. How do you do this using SwiftUI
?
相关问题
- SwiftUI: UIImage (QRCode) does not load after call
- Android ContextMenu for View (Not Activity)
- In a UIViewControllerRepresentable, how can I pass
- How to change default background color for TChromi
- How do you preview a view containing a binding to
相关文章
- SwiftUI ForEach 'identified(by:)' is depre
- Set color of 'empty' area of ListView in A
- What the logcat message: “E/MoreInfoHPW_ViewGroup(
- SwiftUI automatically scroll to bottom in ScrollVi
- Type of expression is ambiguous without more conte
- SwiftUI - Vertical Centering Content inside Scroll
- NavigationView and NavigationLink on button click
- Do I need NO LOCK in my CREATE View query
Would this solution work?:
add following line to SceneDelegate: window.rootViewController?.view.backgroundColor = .black
Fill the entire screen
var body : some View{
}
You can do something like:
around your view.
eg. from the default template (I am encompassing it around a Group):
To add some opacity to it, you can add the
.opacity
method as well:You can also make use of the inspect element of the view by CMD + click on the View and clicking
Show SwiftUI Inspector
>Background
> Your ColorI like to declare a modifier for changing the background color of a view.
Then I use the modifier by passing in a color to a view.
Screen's Background Color
(As of Xcode Version 11.1 (11A1027)
I'm not sure if the original poster meant the background color of the entire screen or of individual views. So I'll just add this answer which is to set the entire screen's background color.
Using ZStack
I added
.edgesIgnoringSafeArea(.all)
otherwise, it will stop at safe area margins.Using Overlay Modifier
Note: It's important to keep the
.edgesIgnoringSafeArea
on just the color so your main content isn't ignoring the safe area edges too.Several possibilities : (SwiftUI / Xcode 11)
1
.background(Color.black) //for system colors
2
.background(Color("green")) //for colors you created in Assets.xcassets
Hope it help :)