Currently trying to determine if there is a way to programmatically set colors in SwiftUI.
Here's an example of the code I have that doesn't work.
ForEach(listItems, id: \.self) { item in
Text(item.name).listRowBackground(Color(item.color))
}
However manually specifying the color like so does.
ForEach(listItems, id: \.self) { item in
Text(item.name).listRowBackground(Color.red)
}
Just wanting to know if anyone else has determined a way dynamically set this with a large selection of colors.
Thanks
Related
I am trying to build a watchOS app that has charting and I can't use Swifts built in charts because I need to support down to version 7 and swift charts are only available for watchOS 9+. So instead I am using a library I found here...
https://github.com/willdale/SwiftUICharts
It has some sample and examples, trying to follow them I was able to get the chart to show up and customize it some, but I can't get the bar chart items to change their color. It seems simple enough, but for whatever reason I can't get it to actually change the color.
I'll provide a simple sample that should help show what's going on.
struct ChartView: View {
var items: [TimeEntries] = [
TimeEntry(dateString: "01/23/2023", entry: 97, timestamp: Date().millisecondsSince1970)]
var body: some View {
let chartData = makeData(items)
BarChart(chartData: chartData)
.touchOverlay(chartData: chartData)
.padding()
}
private func makeData(_ items: [TimeEntries]) -> BarChartData {
var data: [BarChartDataPoint] = [BarChartDataPoint]()
for item in items {
let stat = BarChartDataPoint(
value: Double(item.entry),
xAxisLabel: "Wed",
date: Date(milliseconds: entry.timestamp),
colour: ColourStyle(colour: Color.purple)
)
data.append(stat)
}
let dataSet = BarDataSet(dataPoints: data)
return BarChartData(dataSets: dataSet)
}
}
That should give me an entry on my bar chart with purple filling, I simplified this for sake of ease of posting, my real data has 7 points in it.
However, what actually happens is I have a single red bar on my chart. I am not using red anywhere in the app at all, but it won't take the color that I specify in the colour property of the BarChartDataPoint.
I know it's based on a library, but hopefully someone here will have used this library and will know what I have done wrong. I'll attach a screenshot of the chart so you can see. Thank you.
I want to setup a very simple accessoryCorner SwiftUI widget, but it displays ugly.
This is my code:
struct WidgetView: View {
#Environment(\.widgetFamily) var widgetFamily
var body: some View {
switch widgetFamily {
case .accessoryCorner:
Image(systemName: "cart")
.widgetLabel {
Text("Label")
}
default:
Text("?")
}
}
}
This yields the following watch face:
For some reason, the image (the cart) is displayed in white color on a nearly white background, i.e. it cannot be seen.
I tried various methods to set a better background, e.g. ZStack with AccessoryWidgetBackground(), background(Color.clear), etc., but none worked.
How to display the image without a background, like the day (DI) in the left upper corner?
I contacted Apple and got the following answer:
We have reviewed your request and have concluded that there is no
supported way to achieve the desired functionality given the currently
shipping system configurations.
I have a list of messages that are stored in an array and then displayed to the screen via a loop, which is within a ScrollView. What i want is when the view is loaded the scroll view is scrolled all the way to the bottom, which i attempted to do with the scrollTo function, however the scroll view stayed in the same position no matter what value i passed into it. Can someone help point me in the right direction on how to do this.
struct MessageList2: View {
#Binding var messages: [String]
var body: some View {
ScrollViewReader { value in
ScrollView() {
VStack(alignment: .leading) {
ForEach(0..<messages.count, id: \.self) { index in
Text(messages[index])
}
}
.onAppear{
value.scrollTo(messages.count)
}
}
}
}
}
Note the 'messages array receives value from another function and works just fine
I tried following examples online that showed how to use the ScrollView and scrollTo() and tried to implement it myself however the view stayed the same. Im assuming that im using the function wrong.
I also tried adding a .id(index) under Text() but that didnt seem to work.
This is an off-by-one error. Note the id's you are assigning to each items are the same the index of the elements. The max index that can appear in messages is messages.count - 1, which means the max possible id is messages.count - 1.
So instead of scrollTo(messages.count), you probably meant to scrollTo(messages.count - 1).
This is my example that I am trying to get to work:
struct ContentView: View {
let links = ["Item 1", "Item 2", "Item 3", "Item 4"]
var body: some View {
NavigationView {
ScrollView {
Text("My Title")
List(links, id: \.self) {
link in
NavigationLink(destination: TestView()) {
Text(link)
.padding(.vertical, 4)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.frame(height: 178)
Text("Some more content here")
}
}
}
}
Note: TestView is just some view with the text hello world on it.
I am trying to copy Apple Music's style of navigation. I tried putting a Button in the NavigationLink but tapping it on the text wouldn't change views, and I couldn't find a way to reliably change the color of the row when tapped, at the same time. Also in some approach, I managed to make it work, but the way the colors animate is different, i.e. it fades from A to B, over ~100ms whereas what I'm trying to achieve is to animate between the states instantly (like in Apple Music).
My current approach is using a List, putting NavigationLinks inside it and then cutting off the whole view by giving it a height. This way I can put it alongside other content.
It's working fine for now, but whenever I click on an row and go back, the row is still highlighted, when it shouldn't. Is there a way to make it so that it deselects when going back to the screen somehow?
I think this bug is being caused by the List being inside a ScrollView, since when I removed ScrollView, the list worked properly, and there wasn't this highlight bug. But I need to be able to put my content with the list, and I don't intend to have a list take up the whole screen.
Is there any way to fix this bug with this approach? I'm also willing for other ways to achieve the same result without using List.
Trying to use ForEach instead ofList?
With a view for row (CustomRow) where you can pass link item and set custom dividing line, background etc ...
ForEach(links, id: \.self) { link in
NavigationLink(destination: TestView()) {
CustomRow(item: link)
}
}
.frame(height: 178)
I'm trying to set a custom style to a group of options belonging to a dropdown or a Radio Group. I'm searching all over and it seems impossible. At least I would like to decrease the size of the letters in the text of each option because is too big! and crashes my UI.
Here's and example of what I need:
RadioGroup {
id: groupOrigin
objectName: "groupOrigin"
Option{
text: "text to display"
//This text default style is what I'm trying to change. Please help!.
}
}
thanks!
for Radio group you can achieve this using custom implementation. Take a label and place it after the radio group did not provide text inside the options tag. So whatever style you want to apply can be done using label.
On label you can set the font size, color and other style parameter you want to apply.
Please check the source code below for this custom radio button. I did this in the QML you can achieve same in C++.
// The Component title.
Label {
id: titleLabel
text: ""
textStyle {
base: SystemDefaults.TextStyles.SmallText
alignment: TextAlignment.Center
}
layoutProperties: StackLayoutProperties {
horizontalAlignment: HorizontalAlignment.Fill
}
}
// The radio group presenting the different curves.
RadioGroup {
id: radioGroup
Option {
text: "Height"
}
}