小编san*_*.gs的帖子

在 macOS 的 SwiftUI 中更改 TextEditor 背景颜色

我想在 macOS 上更改 SwiftUI 文本编辑器的背景颜色。下面的代码(用于 iOS)是否有适用于 NSTextField 而不是 UITextView 的变体?

谢谢。

struct ContentView: View {
    init() {
        UITextView.appearance().backgroundColor = .clear
    }

    var body: some View {
        TextEditor(text: .constant("Placeholder"))
            .background(Color.red)
    }
}
Run Code Online (Sandbox Code Playgroud)

macos nstextfield swift swiftui

9
推荐指数
1
解决办法
983
查看次数

如果 Button 具有 buttonStyle,则 SwiftUI 键盘快捷键不起作用

这有效

Button(action: {
   print("pressed")
}){
   Text("Button")
}
.keyboardShortcut("B", modifiers: .command)
Run Code Online (Sandbox Code Playgroud)

这不

Button(action: {
   print("pressed")
}){
   Text("Button")
}
.buttonStyle(PlainButtonStyle())
.keyboardShortcut("B", modifiers: .command)
Run Code Online (Sandbox Code Playgroud)

有没有其他人经历过这个

swift swiftui

9
推荐指数
1
解决办法
233
查看次数

Swift UI 列表动画,用于扩展具有动态高度的单元格

我试图在用户点击单元格时为 swiftUI 列表中的单元格展开动画。然而动画有缺陷。

我遇到了这个答案(/sf/answers/4261171841/)来帮助制作动画,但是它需要已经知道单元格展开后的预期高度。就我而言,这取决于以下数据note.text

List{
    ForEach(notes){ note in
        VStack{
            HStack{
                Text(note.title)
                    .fontWeight(.bold)
                    .foregroundColor(.gray)
                Spacer()
                Image(systemName: "chevron.right.circle")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .foregroundColor(.gray)
                    .frame(width: 20, height: 20)
            }
            .padding(.vertical)
            .contentShape(Rectangle())
            .onTapGesture{
                selectedNote = note
            }
            if isSelected(note) {
                Text(note.text)
            }
        }
        .padding([.bottom, .leading, .trailing], 20)
        .background(
            RoundedRectangle(cornerRadius: 25, style: .continuous)
                .foregroundColor(isSelected(noteSection) ? .red : .clear)
        )
        .animation(.default)
    }
    .onMove(perform: move)
}
Run Code Online (Sandbox Code Playgroud)

正如您从上面的代码中看到的,我需要使用一个列表,以便我可以使用 .onMove 功能。

有没有办法在链接的答案中获得相同的平滑列表动画,但对于动态扩展尺寸?

谢谢。

animation swiftui

7
推荐指数
1
解决办法
984
查看次数

UIImageView 可表示未调整大小

I can't find a way to make a UIImageView wrapped in a UIViewRepresentable be sized to fit the frame. It always resizes beyond the screen no matter what content Mode or clipping or explcit framing I do. (The image dimensions are much larger than the device screen frame)

To clarify: I need to use UIImageView due to some subview positioning I have to do down the line and various other reasons.

Here's a paired down example:

struct ImageView: UIViewRepresentable { …
Run Code Online (Sandbox Code Playgroud)

swiftui uiviewrepresentable

7
推荐指数
1
解决办法
1137
查看次数

SwiftUI .sheet 解包可选值时意外发现 nil

问题

下面的例子比我能解释的更好地突出了我的问题。在呈现表格之前,我明确地为可选变量赋予一个值。该表需要一个非可选变量来初始化,它不注册该值并表示它为零。我不明白为什么如果我只在给可选值赋予值后调用工作表,就会出现这种情况。任何帮助将不胜感激。谢谢!

我尝试过的

在我替换的示例中:

.sheet(isPresented: $showModalView, content: {
     EditBookView(book: editingBook!) //Fatal error here
})
Run Code Online (Sandbox Code Playgroud)

和:

.sheet(isPresented: $showModalView, content: {
     if let book = editingBook {
         EditBookView(book: book)
     }
})
Run Code Online (Sandbox Code Playgroud)

但是,这仅显示一张空表(意味着它editingBook是空的)。但是,有趣的是,当我关闭这个空工作表并选择列表中的另一个项目时,视图会按预期显示。

可重现的例子

import SwiftUI

struct Book: Identifiable {
    var id: UUID
    var title: String
    
    init(title: String){
        self.title = title
        self.id = UUID()
    }
}

struct ContentView: View {
    
    @State var books = [Book]()
    @State var showModalView = false
    
    @State var editingBook: Book? = nil
    
    var body: some View { …
Run Code Online (Sandbox Code Playgroud)

swiftui

5
推荐指数
1
解决办法
1257
查看次数

NSTextList 格式化

我试图弄清楚如何使用 NSTextList 但除了这个 SO 问题此博客中的评论之外,在网上几乎找不到有用的信息。

使用这个我已经能够创建一个粗略的例子来弄清楚:

    let textView = NSTextView()
    
    //Create NSTextList
    let list = NSTextList(markerFormat: .decimal, options: 0)
    list.startingItemNumber = 1
    
    //Create NSParagraphStyle with text list
    let paragraphStyle = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
    paragraphStyle.textLists = [list]
    
    let attributes = [NSAttributedString.Key.paragraphStyle : paragraphStyle]
    
    //Add attributes to list block
    let nsmutableAttributedString = NSMutableAttributedString(string: "\t\(list.marker(forItemNumber: 1))\tList Item 1 ", attributes: attributes)
    
    textView.textStorage?.setAttributedString(nsmutableAttributedString)
    
    return textView
Run Code Online (Sandbox Code Playgroud)

这效果很好,当应用程序运行时,按下enter会创建一个新的格式正确的行,按下tab会正确缩进列表项。

一切都很好...但是,标记周围的间距似乎不正确,并且不能反映您在 TextEdit 中获得的内容。

这是文本编辑,注意标记周围的间距 展示一个

这是我的文本视图,请注意标记周围较大的间距,在执行下面突出显示的过程后,将恢复为“默认”文本编辑间距...

在此输入图像描述

我希望从中得到三件事:

  1. 我在某种程度上做错了吗?我很确定我必须\t …

macos nstextview appkit swift

5
推荐指数
1
解决办法
873
查看次数

如何在 SwiftUI NavigationLink 中移除不透明动画

点击 NavigationLink 时,它会略微降低不透明度。有没有办法禁用它。我尝试使用,.buttonStyle(PlainButtonStyle())但没有达到预期的效果。

它嵌入在滚动视图中(为了可定制性,优先于列表):

ScrollView {
    ForEach(items){ item in
        NavigationLink(destination: DetailView()){
            HStack{
                Text("title")
                Spacer()
                Image(systemName: "chevron.right")
            }
            .padding()
            .background(
                RoundedRectangle(cornerRadius: 10, style: continuous)
                    .foregroundColor(Color.gray)
            )
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

navigationview swiftui navigationlink

3
推荐指数
1
解决办法
1384
查看次数