I want to test the code below in live preview, but it seems not to work. In simulator, the button can change the text well, but not in live preview. How to fix it? Thanks in advance.
import SwiftUI
struct PreviewOnchangeTest: View {
@Binding var toggle: Bool
@State var text = "nice"
var body: some View {
VStack{
Text("\(text)")
.onChange(of: toggle, perform: { value in
text = toggle.description
})
Button(action: {toggle.toggle()}, label: {
Text("change")
	})
}
}
}
struct PreviewOnchangeTest_Previews: PreviewProvider {
@State static var toggle = true
static var previews: some View {
PreviewOnchangeTest(toggle: $toggle)
}
}
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I downloaded sf symbol3 today. My Xcode is 12.5. When I write a simple code like:
Image(systemName: "gearshape")
everything is ok. However when the code turns to :
Image(systemName: "gearshape.circle")
nothing will be displayed.
Do I miss anything? Thanks in advance.
struct PickerStyleView: View {
@State private var num = 0
var body: some View {
Form{
Text("Number")
Picker(String(num), selection: $num, content: {
Text("5").tag(5)
Text("10").tag(10)
Text("15").tag(15)
Text("20").tag(20)
})
.pickerStyle(InlinePickerStyle())
}
}
}
Using InlinePickerStyle() and WheelPickerStyle() seems to be no difference.
What does "InlinePickerStyle()" refer to? And which case is "InlinePickerStyle()" suitable for in practice? Any help will be appreciated.
I'm writing an app which allow users to pick images from photo library by swiftUI. I wrap PHPickerViewController in representable to achieve the functionality. The benefit is that users don’t need to explicitly authorize my app to select photos.
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])
Currently, I get images by picker(::) method above and save images in my app in order to reload images the next time users launch the app without needs to request authorization.
Obviously, if the app stores references to the selected images, rather than images themselves, it can prevent from taking up large amounts of space. Is it meaning that I have to prompt to users for requesting authorization to access the library? Is there any approach only to use references and reload images which were selected by users previously from photo library using PHPickerViewController, supporting no need to request authorization.
I want to use pencil kit in my swiftui app. I encapsulate PKCanvasView in UIViewRepresentable. I can draw on canvas with only one stroke. However, the image will disappear immediately when I release the finger.
Am I missing anything?
import SwiftUI
import PencilKit
struct ContentView: View {
var body: some View {
CanvasView()
}
}
struct CanvasView: UIViewRepresentable {
func makeUIView(context: Context) -> PKCanvasView {
let canvas = PKCanvasView()
canvas.tool = PKInkingTool(.pen, color: .green, width: 10)
canvas.drawingPolicy = .anyInput
return canvas
}
func updateUIView(_ uiView: PKCanvasView, context: Context) {
}
}
[https://developer.apple.com/fonts/system-fonts/) shows dozens of system fonts. And it says "Apple platforms come with many preinstalled fonts that can be used by your app’s user interface. " on the top of the web page. Does it mean I can use any of them in my iOS app legally? Does it involve font license issue? What else should I pay attention to use system fonts legally?
I want to perform some code when the form is tapped. However the button in the form is affected by the onTapGesture either. I want the button tapped to execute the action block, not the onTapGesture block. How should I do? Thanks in advance.
struct TextFieldPopupView: View {
@State private var text = ""
@State private var isON = false
var body: some View {
Form{
Text("Hello")
TextField("Hello", text: $text)
.textFieldStyle(RoundedBorderTextFieldStyle())
Button(action: {
print("Button")
}, label: {Text("Button")})
Toggle(isOn: $isON, label: {
Text("Toggle")
})
}
.onTapGesture {
print("Form")
}
}
}
I wrote some code using LazyVGrid.
struct LazyVStackTestView: View {
let columnGridItem = Array(repeating: GridItem(), count: 3)
var body: some View {
GeometryReader{geo in
HStack{
Rectangle().frame(maxWidth: 300,maxHeight: 100)
LazyVGrid(columns: columnGridItem){
ForEach(1..<13) { btn in
Image(systemName: "plusminus.circle")
.resizable()
.scaledToFit()
.clipShape(Circle())
}
}
.padding()
.frame(maxWidth: geo.size.height*0.6, maxHeight: geo.size.height)
}.border(Color.black)
.padding()
}.border(Color.blue)
}
}
When run it on simulator(iPhone 11) in horizontal orientation, I get a 4rows3columns button pad that is what I want. But when I turn the simulator to vertical orientation and back to horizontal orientation immediately, the button pad becomes 3rows3columns! What had happened?
Any help will be appreciated.
I have a swiftui view that displays a picture which users select from Photo app and shows some texts below the picture. I want to dynamically tune the background color of the view depending on the picture major color. Say, if users select a grass or forest image, the background auto changes to green. Is there any best practice?
I need to use custom font in my app. While the built-in standard font will align in center automatically, the custom font seems to align "top"(seeing red border part) instead of "center"(blue border part). Is there any approach to fix it?
struct ContentView: View {
var body: some View {
VStack(spacing: 20){
Text("SEPTEMBER")
.font(.largeTitle)
.border(.blue)
Text("SEPTEMBER")
.font(Font.custom("Arial Hebrew", size: 20))
.border(.red)
}
}
}
I'm creating a launch screen by storyboard for a swiftui app. When I rotate the iPhone 14 to landscape, the safe area has insets on vertical, without any inset on horizontal(shown as the image below). IMO, the safe area should have inset on leading, trailing and bottom edge in this case. Do I miss anything?
I want to get the loop times info in playground. But it seems to only display the latest result for a loop on the right side. Do I miss anything? Xcode 15.0.1
import Cocoa
for i in 1...5{
print("\(i)")
}
I have a screenshot:
Topic:
Developer Tools & Services
SubTopic:
Swift Playground
Tags:
Swift Playground
Playground Support
I have a list. When I click the show button in any of rows, the long text appears and the height of row auto gets taller enough to show all of the long text.
However, when the list is in "edit" mode, the height of row can't auto get taller enough to show all of the long text. The long text is truncated. I have to push the list to the top manually and release, then the height of row auto gets taller and the long text can be completely displayed again. Is there any way to achieve the "auto" adjustment effect just the same as the "non-edit" mode?
struct ListEditTest: View {
@State private var selects: Set<Int> = []
var body: some View {
VStack{
EditButton()
List(selection: $selects){
ForEach(1..<5){
ItemInList(num: $0)
}
}
.listStyle(PlainListStyle())
}
}
}
struct ItemInList: View{
let num: Int
@State private var showDetail = false
var body: some View{
HStack{
Text("NO.\(num)")
Text("HelloWorld")
if showDetail{
Text("TooManyWordsTooManyWordsTooManyWordsTooManyWordsTooManyWordsTooManyWordsTooManyWordsTooManyWords")
Button {
showDetail.toggle()
} label: {
Text("Hide")
}
}else{
Button {
showDetail.toggle()
} label: {
Text("Show")
}
}
}
.buttonStyle(BorderlessButtonStyle())
}
}
I drag a image.jpg into the assets.xcassets. And I want to read the jpg data in my code by
"var imageData = NSDataAsset(name: "image")?.data"
However, I get an error: "CoreUI: attempting to lookup a named data 'image' with a type that is not a data type in the AssetCatalog". And "imageData" is always nil.
Do I make a mistake or miss sth? Thanks in advance.
struct ProblemSegmentTest: View {
@State private var select = 0
var body: some View {
VStack {
Picker("Test", selection: $select) {
Text("A").tag(0)
Text("B").tag(1)
Text("C").tag(2)
}
.pickerStyle(SegmentedPickerStyle())
}
.onTapGesture {
print("test")
}
}
}
I want to do sth when the picker is taped. However, when I apply ".onTapGesture" to the vstack, the picker's options can't be selected any more. Any help will be appreciated.