有没有办法让列表的高度自动匹配其内容?
我有一个列表,将显示许多项目(我事先不知道有多少)。默认情况下,当我创建列表时,它没有获得足够的高度,而是显示 1-3 个项目并滚动以显示更多项目。我想禁用滚动行为。
我尝试将框架高度硬编码为某个大值,我确信该值适合所有项目,但这并不理想。我还可以根据 itemHeight * numItems 计算框架高度(如果我将所有项目本身设置为指定高度),但在我的情况下,项目可以具有不同的高度。
也许有一个更简单的解决方案?
我查看了文档,还尝试了 .fixedSize()、不同的列表样式等。这些对我不起作用。
我没有成功在 a 中添加视图Form而不嵌入 a 中Section,所以我尝试Section为Section相同的背景设置“不可见”设置Form,但是有一些分隔符我也必须隐藏,我发现这个UITableView.appearance().separatorColor = UIColor(named: "Background")设置为分隔符的颜色与Form背景相同,但问题是这适用于整体Form,我不希望这样:(
知道如何在Form没有 a 的情况下添加视图Section,或者如何“隐藏”该部分而不影响Sections中的其他部分Form吗?
struct ContentView: View {
init() {
UITableView.appearance().separatorColor = UIColor(named: "Background")
}
var body: some View {
Form {
Section {
Text("text1")
Text("text2")
Text("text3")
}
Text("View without section")
.font(.title)
.listRowBackground(Color("Background"))
}
}
}
Run Code Online (Sandbox Code Playgroud)
我使用 aNavigationLink从“View1”导航到“View2”,在第二个视图上,后退按钮获取上一个视图的标题
但是,如果前一个视图的标题很长,则后退按钮将获取文本“后退”
我怎么能改变那个“返回”文本?
我想让我的应用程序以多种语言提供,但是当手机的语言改变时,“返回”似乎没有改变
struct ContentView: View {
var body: some View {
return NavigationView {
VStack {
Text("View1")
NavigationLink(destination: Text("View2").navigationBarTitle("Title View2", displayMode: .inline)) {
Text("NavigationLink")
}
}.navigationBarTitle("Title View1")
}
}
}
Run Code Online (Sandbox Code Playgroud)
PS:我想保留这个功能,我只想更改用于后退按钮的语言
ContentView2model.value更改时不刷新视图,如果直接Model符合ObservableObject而不是继承SuperModel则可以正常工作
class SuperModel: ObservableObject {
}
class Model: SuperModel {
@Published var value = ""
}
struct ContentView2: View {
@ObservedObject var model = Model()
var body: some View {
VStack {
Text(model.value)
Button("change value") {
self.model.value = "\(Int.random(in: 1...10))"
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想在收到请求的数据后呈现一个视图,像这样
var body: some View {
VStack {
Text("Company ID")
TextField($companyID).textFieldStyle(.roundedBorder)
URLSession.shared.dataTask(with: url) { (data, _, _) in
guard let data = data else { return }
DispatchQueue.main.async {
self.presentation(Modal(LogonView(), onDismiss: {
print("dismiss")
}))
}
}.resume()
}
}
Run Code Online (Sandbox Code Playgroud) 我想改变这种“浅灰色”背景表格的颜色,但.foregroundColor(Color.blue)并.background(Color.blue)不会似乎工作
struct ContentView : View {
@State var value = ""
var body: some View {
Form {
Section(header: Text("First Name")) {
TextField($value)
}
Section(header: Text("Last Name")) {
TextField($value)
}
}.foregroundColor(Color.blue)
}
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试过
NavigationView {
Form {
Section {
Text("Error message!")
}.background(Color.red.opacity(0.2))
Section {
Text("Label 1")
}
Section {
Text("Label 2")
}
}.navigationBarTitle("title", displayMode: NavigationBarItem.TitleDisplayMode.inline)
}
Run Code Online (Sandbox Code Playgroud)
但是,似乎有一个预定义的填充,所以我无法填充边距
我正在尝试为弹出窗口设置一定的大小或使其适应其内容
我尝试从弹出窗口更改视图的框架,但它似乎不起作用
Button("Popover") {
self.popover7.toggle()
}.popover(isPresented: self.$popover7, arrowEdge: .bottom) {
PopoverView().frame(width: 100, height: 100, alignment: .center)
}
Run Code Online (Sandbox Code Playgroud)
我想实现在 iPad 的日历应用程序中发现的这种行为
在下面的代码中,当按下Button时,应该在控制台中打印“ Test”,但不是这样。该事件不是通过发布者发送的。知道Xcode 11 Beta 5中的PassthroughSubject发生了什么吗?(在Xcode 11 Beta 4中效果很好)
var body: some View {
let publisher = PassthroughSubject<String, Never>()
publisher.sink { (str) in
print(str)
}
return Button("OK") {
publisher.send("Test")
}
}
Run Code Online (Sandbox Code Playgroud)
PS:我知道按下按钮时还有其他打印字符串的方法,我只想展示一个简单的发送-接收示例
我正在尝试UITableView在 SwiftUI 应用程序中使用
struct UIList: UIViewRepresentable {
var rows: [String]
func makeUIView(context: Context) -> UITableView {
let collectionView = UITableView(frame: .zero, style: .plain)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.dataSource = context.coordinator
collectionView.delegate = context.coordinator
collectionView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
return collectionView
}
func updateUIView(_ uiView: UITableView, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator(rows: rows)
}
class Coordinator: NSObject, UITableViewDataSource, UITableViewDelegate {
var rows: [String]
init(rows: [String]) {
self.rows = rows
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个List所有行都具有相同高度的
struct ContentView: View {
@State var content = ["row1", "row2\nrow2\nrow2", "row3"]
var body: some View {
List {
ForEach(0..<content.count, id: \.self) { index in
Text(self.content[index])
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我按下按钮时,我希望第二个矩形(绿色)在第一个矩形(红色)上上下移动
struct ContentView: View {
@State var visible = false
var body: some View {
return HStack {
Button("button") {
self.visible.toggle()
}
ZStack {
Rectangle()
.frame(width: 100, height: 100)
.foregroundColor(Color.red)
if visible {
Rectangle()
.frame(width: 100, height: 100)
.foregroundColor(Color.green)
.transition(.move(edge: .bottom))
.animation(.linear(duration: 2))
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当绿色矩形出现时,它出现在红色矩形上方,但当它消失时,移动过渡出现在红色矩形后面。