ZStack {
VStack {
Text("some text")
Button("Ok") {}
.foregroundColor(.cyan)
.padding()
}
.padding()
}
.background(.red)
.border(.blue, width: 5)
.cornerRadius(20)
Run Code Online (Sandbox Code Playgroud)
我希望整个视图具有带圆角的蓝色边框(而不是与圆形蓝色边框重叠的红色正方形。如何实现?我似乎已经尝试了排序修改器的所有变体。
以下代码工作正常。所以真的,我很好......但我想了解 ViewModifiers......所以我的目标是将不变的东西与动态的东西分开,以创建一个.cardify()自定义修饰符来调用形状视图.
struct SetCard: View {
let pips: Int
let shape: SetCardShape
let color: Color
let shading: Double
let isSelected: Bool
var body: some View {
ZStack {
VStack {
ForEach( 0..<pips ) { _ in
ZStack {
getShape(self.shape).opacity(self.shading).foregroundColor(self.color)
getShape(self.shape).stroke().foregroundColor(self.color)
}
}
}
.padding() // for shape in RoundedRect
RoundedRectangle(cornerRadius: 10).stroke(lineWidth: isSelected ? 3.0 : 1.0).foregroundColor(.orange)
}
.scaleEffect(isSelected ? 0.60 : 1.0 )
.padding() // for spacing between cards
}
}
Run Code Online (Sandbox Code Playgroud)
同样,出于学术/学习原因,我想简化这个结构,并使用自定义修饰符将主要内容转换为标准卡片。
下面的代码块仅在我注释掉ViewModifier 结构中的第二content行 …
有没有办法创建修改器来更新@State private var正在修改的视图中的a ?
我有一个自定义视图,它返回Text带有“动态”背景颜色的 a 或Circle带有“动态”前景色的 a。
struct ChildView: View {
var theText = ""
@State private var color = Color(.purple)
var body: some View {
HStack {
if theText.isEmpty { // If there's no theText, a Circle is created
Circle()
.foregroundColor(color)
.frame(width: 100, height: 100)
} else { // If theText is provided, a Text is created
Text(theText)
.padding()
.background(RoundedRectangle(cornerRadius: 25.0)
.foregroundColor(color))
.foregroundColor(.white)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序的不同部分重复使用这个视图。如您所见,我需要指定的唯一参数是theText. 因此,创建此 ChildView …
我正在尝试使用.quickLookPreviewiOS 14、macOS 11 macCatalyst 14 中引入的视图修改器,但Value of type 'some View' has no member 'quickLookPreview'每次尝试在 macOS 或 mac Catalyst 目标上使用该修改器时都会收到此错误。在 iOS 上,这工作得很好。在 Mac 上呈现此修改器的正确方法是什么?
import SwiftUI
import QuickLook
struct ContentView: View {
@State var documentUrl: URL?
var body: some View {
NavigationView {
ZStack {
Button("Press Me", action: {
documentUrl = URL(fileURLWithPath: "somelocalfileURLPath")
})
}
.navigationTitle("Open File")
.quickLookPreview($documentUrl)
}
}
}
Run Code Online (Sandbox Code Playgroud)
Xcode 13.1 MacOS 12.1
我正在尝试创建这个修改器:
struct CustomTextBorder: ViewModifier {
func body(content: Content) -> some View {
return content
.font(.largeTitle)
.padding()
.overlay(
RoundedRectangle(cornerRadius: 15)
.stroke(lineWidth: 2)
)
.foregroundColor(.blue)
}
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我收到Type 'CustomTextBorder' does not conform to protocol 'ViewModifier'错误。
看来我必须补充一下:
typealias Body = <#type#>
但是,我看到修改器的创建就像我最初在这里所做的那样,而无需提供typealias Body...
该修改器在这里起作用:
https://www.simpleswiftguide.com/how-to-make-custom-view-modifiers-in-swiftui/
为什么它对我不起作用?
我怎样才能让这个修改器起作用?为什么它对某些人有效而对其他人无效?这取决于项目的目标是什么吗?我的目标是 iOS 15。
我的观点之一有这段代码:
struct TextFieldClearButton: ViewModifier {
@Binding var text: String
func body(content: Content) -> some View {
HStack {
content
if !text.isEmpty {
Button(
action: { self.text = "" },
label: {
Image(systemName: "delete.left")
.foregroundColor(Color(UIColor.opaqueSeparator))
}
)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到两个错误:
Type 'TextFieldClearButton' does not conform to protocol 'ViewModifier'Static method 'buildBlock' requires that 'Content' conform to 'View'我怎样才能摆脱这些错误并使这个修改器编译?
看来我ViewModifier根本用不了啊 也添加超级简单的大小写错误??:
我想添加一个 ViewModifier,如 Paul Hudson 的以下教程中所述(https://www.hackingwithswift.com/books/ios-swiftui/custom-modifiers)。我的视图修改器是:
import SwiftUI
struct Watermark: ViewModifier {
var text: String
func body(content: Content) -> some View {
ZStack(alignment: .bottomTrailing) {
content
Text(text)
.font(.caption)
.foregroundColor(.white)
.padding(5)
.background(Color.black)
}
}
}
extension View {
func watermarked(with text: String) -> some View {
self.modifier(Watermark(text: text))
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
我尝试在另一个项目中重现这一点,但它只是按预期工作。我已经尝试清理构建文件夹,删除派生数据,重新启动 Xcode,重新启动 Mac。
关于如何解决这个问题有什么想法吗?
设置:
我有一个View可以显示警报的 SwiftUI。警报由AlertManager单例通过设置title和/或message其已发布的属性来提供@Published var nextAlertMessage = ErrorMessage(title: nil, message: nil)。该View拥有财产@State private var presentingAlert = false。
当以下修饰符应用于 时,此功能有效View:
.onAppear() {
if !(alertManager.nextAlertMessage.title == nil && alertManager.nextAlertMessage.message == nil) {
presentingAlert = true
}
}
.onChange(of: alertManager.nextAlertMessage) { alertMessage in
presentingAlert = alertMessage.title != nil || alertMessage.message != nil
}
.alert(alertManager.nextAlertMessage.joinedTitle, isPresented: $presentingAlert) {
Button("OK", role: .cancel) {
alertManager.alertConfirmed()
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
由于警报也将在其他视图中呈现,因此我编写了以下自定义视图修饰符:
struct ShowAlert: ViewModifier …Run Code Online (Sandbox Code Playgroud) viewmodifier ×8
swiftui ×7
swift ×4
mac-catalyst ×1
macos ×1
quicklook ×1
stroke ×1
struct ×1
xcode ×1