gpt4 book ai didi

ios - 删除 UISearchBar 文本字段周围的填充

转载 作者:行者123 更新时间:2023-12-04 21:05:32 26 4
gpt4 key购买 nike

我试图左对齐 UISearchBar 中的文本字段.

我说的是下面紫色的区域。

enter image description here

我已经能够通过将背景设置为 .clear 来隐藏它但是我现在希望文本字段与上面文本的前缘对齐。我不确定我应该调整什么属性以正确左对齐整个搜索字段。

enter image description here

最佳答案

不幸的是,没有办法可靠地移除这个填充。目前(从 iOS 13 开始)水平填充为 8 pts,垂直填充为 10 pts。

您可以简单地将前导/尾随约束设置为“Discogs|Search” View + 8 pts 以使其显示相同的大小。

我需要做一些类似于 wrap UISearchBar 的事情在 SwiftUI 中,而是使用了 padding UISearchBar 上的修饰符UIViewRepresentable像这样:

struct SearchBar: View {
private var title: String = "Search"
@Binding private var text: String
private var onEditingChanged: (Bool) -> Void

init(
_ title: String,
text: Binding<String>,
onEditingChanged: @escaping (Bool) -> Void = { _ in }
) {
self.title = title
self._text = text
self.onEditingChanged = onEditingChanged
}

var body: some View {
__SearchBar(title, text: $text, onEditingChanged: onEditingChanged)
.padding(.horizontal, -8)
.padding(.vertical, -10)
}
}

private struct __SearchBar: UIViewRepresentable {
private var placeHolder: String = "Search"
@Binding private var text: String
private var onEditingChanged: (Bool) -> Void

init(
_ title: String,
text: Binding<String>,
onEditingChanged: @escaping (Bool) -> Void
) {
self.placeHolder = title
self._text = text
self.onEditingChanged = onEditingChanged
}

final class Coordinator: NSObject, UISearchBarDelegate {
private let parent: __SearchBar
@Binding var text: String

init(parent: __SearchBar, text: Binding<String>) {
self.parent = parent
_text = text
}

func searchBar(
_ searchBar: UISearchBar,
textDidChange searchText: String
) {
text = searchText
}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
parent.onEditingChanged(true)
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
parent.onEditingChanged(false)
}
}

func makeCoordinator() -> Coordinator {
Coordinator(parent: self, text: $text)
}

func makeUIView(context: Context) -> UISearchBar {
let searchBar = UISearchBar(frame: .zero)
searchBar.placeholder = placeHolder
searchBar.searchBarStyle = .minimal
searchBar.delegate = context.coordinator
return searchBar
}

func updateUIView(_ uiView: UISearchBar, context: Context) {
uiView.text = text
}
}

关于ios - 删除 UISearchBar 文本字段周围的填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55636460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com