I am trying to modify the navigation bar text attributes in SwiftUI (to add a text shadow) and have hit a wall trying to understand why I'm receiving run-time crashes in the simulator. There was another thread (ref: below) that was able to solve changing the font type using init() to modify the appearance, however trying to use init to modify the Text("") method to add a shadow results in a crash.
I've also tried extracting the Text("NavBarTitle") into its own method, then applying modifiers (no luck there). As seen in my code I've tried extracting the text into a variable results in a crash. Even just applying the modifiers directly causes a crash.
I'm not experienced enough in SwiftUI to call this a bug but it really feels like one.
Thanks for your help in advance!
import SwiftUI
struct ContentView: View {
init() {
UINavigationBar.appearance().largeTitleTextAttributes = [.shadow: 5]
}
let navigationBarText: Text = Text("Navigation Bar")
var body: some View {
NavigationView {
VStack {
Text("Hello, World!")
}
.navigationBarTitle(navigationBarText)
}
}
}
ref: https://stackoverflow.com/a/57632229/1514009
It should be provided NSShadow object instead of number, as below
init() {
let shadow = NSShadow()
shadow.shadowOffset = CGSize(width: 5, height: 2)
UINavigationBar.appearance().largeTitleTextAttributes = [.shadow: shadow]
}
Related
First of all playground example that demonstrates the problem:
import UIKit
import SwiftUI
import PlaygroundSupport
struct MyList: View {
var body: some View {
content
}
private var content: some View {
List {
Section {
Text("hello")
}
.background(Color.red) // This only changes small area around text. Looking for Workaround 2
}
.background(Color.blue) // This doesn't work (Workaround 1)
.foregroundColor(Color.yellow)
}
init() {
// Workaround 1 - works
UITableView.appearance().backgroundColor = .blue
// Workaround 2 - nothing below helps
UITableView.appearance().sectionIndexTrackingBackgroundColor = .green
UITableView.appearance().sectionIndexBackgroundColor = .green
UITableViewCell.appearance().backgroundColor = .green
UITableViewCell.appearance().backgroundView = nil
UITableViewCell.appearance().backgroundConfiguration = .clear()
}
}
let viewController = UIHostingController(rootView: MyList())
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = viewController
Explanation:
View has a List, which I need to change background for. There's a well-known workaround to change it via UITableView.appearance().backgroundColor, which is what I do. So far so good.
I also need to change the background of the section. In the image below I want the entire section to be red, not white. White should not exist at all:
But I can't find the way to achieve this. If I add .background(Color.red) to the Section, it only changes the small area around the text. OK, but if the white area is not part of List, and not Section then what is the rest of the white area?
I tried various workarounds found in SO, as listed under // Workaround 2 - nothing below helps. None of them help.
So my question is, simply put, how do I get rid of white area in this layout.
Note:
I would prefer to not switch away from List, unless it's the only option.
Answers involving introspect are fine too.
Change:
.background(Color.red) // This only changes small area around text. Looking for Workaround 2
To:
.listRowBackground(Color.red)
Reference
I run into an issue I have no idea how to solve. I plan to have a shared code base for macOS and iOS with SwiftUI (as much as possible).
But already with something simple like setting background color for a text field there is a difference
Following code snippet:
struct NewTextField : View
{
#State var d:String = ""
var body: some View
{
HStack(content:
{
TextField("Date: ", text:$d)
.foregroundColor(.black)
.background(Color.green)
/// ... more views
}
}
}
This is simplified; I have multiple textfields on the real application with background colors depending on the content.
on iOS it looks ok; the full textfield background is set to the desired color; on macOS the TextField appear with white background and colored border.
What I'm doing wrong ?
just to leave it not unanswered:
added
.textFieldStyle(PlainTextFieldStyle())
and it create the desired effect
TextField("Date: ", text:$d)
.textFieldStyle(PlainTextFieldStyle())
.foregroundColor(.black)
.background(Color.green)
based on this post: SwiftUI customized text field in OSX
I am currently working with Lists in SwiftUI.
Problem: A default View in SwiftUI has a white background Color. However, when adding a List to it, the Background Color changes to systemGray6 while the List Cells take the white bgColor.
Goal: I would like to have a List with white bgColor and Cells with systemGray6 bgcolor.
I accomplished to change the Background Color of the List Rows :
List(users) { user in
// ...
}
.listRowBackground(Color(UIColor.systemGray6)
However, I can't find a solution for changing the Screen's bgColor back to white. I tried the obvious stuff like .background(Color.white).
Does anyone has a solution for this issue?
Thanks for your help.
you can change those colors as you wants, here a show example:(tested with Xcode 12.1 / iOS 14.1)
struct ContentView: View {
init() {
UITableView.appearance().backgroundColor = UIColor.white
}
var body: some View {
List
{
ForEach(0 ..< 20) { index in
Text("Row \(index)")
.listRowBackground(Color(UIColor.systemGray6))
}
}
}
}
Hobbyist ios coder here...
I have an old Objective C project I am trying to recreate in swiftui.
When I click a button, it draws a CGRect and fills in the peramiters of image source from the IBAction I wrote out that i can then drag arround the screen.
Old way I used to do it
How do i do this in SwiftUI?
My empty SwiftUI Button Code
I currently have an image drawn within a VStack that I can move arround and resize, but I want it to not exist on app load, but for it and others to be drawn on user request. IE button press.
I have no idea what it is im supposed to search for to find this answer as searching for button and image instantly gives me tutorials on how to add images to a button, not a button that draws a fresh interactable element on the view.
Thank you to anyone who can shed more light on this.
EDIT - On pressing the button a second time I need the image to spawn again so there would be multiple instances of it.
I'm not 100% sure if I understand this correctly, but it's simply a matter of displaying an image when a button is clicked?
You already said that you got an image with the desired behavior, but you don't want it to be there from the beginning. I think you are looking for an alternative in SwiftUI for addSubview, but there is no direct one.
What you might be looking for is #State in SwiftUI. Changing a #State variable will force your view to redraw itself. (There are more then just #State variables that to this)
You can draw your Image based on a condition. On your button click you could change the state variable that your condition is based on to redraw your view and to display your Image. I use the toggle() function in my example, so clicking the Button a second time will result in removing the image.
import SwiftUI
struct ContentView: View {
#State private var showImage = false
var body: some View {
VStack {
if showImage {
Image(systemName: "gear") // Your working image
}
Button(action: {
self.showImage.toggle()
}, label: {
Text("draw image")
})
}
}
}
Here is an example how you could add more and more Images when clicking the button again and again. I changed the Type of the #State variable to an array and then iterate over that array via a ForEach.
import SwiftUI
struct ContentView: View {
#State private var images: [UUID] = []
var body: some View {
VStack {
ForEach(images, id: \.self) { _ in
Image(systemName: "gear") // Your working image
}
Button(action: {
self.images.append(UUID())
}, label: {
Text("draw image")
})
}
}
}
I need to be able to see through my SwiftUI Views as well as the UIHosting Controller presenting them in order to see a background image underneath them. The code below still shows a white rectangle under my SwiftUI View. Paul Hudson says to use edgesIgnoringSafeArea but I cannot use that as my SwiftUIView is embedded in a larger UI scheme. Is there any way to make this white rectangle I am seeing clear?
var body: some View {
ZStack {
VStack {
//omitted
}
}.background(Color.clear)
}
If you are using a UIHostingController, you need to set the backgroundColor property of its view to .clear as well.
yourHostingController.view.backgroundColor = .clear