gpt4 book ai didi

How to correctly handle full view resizing in SwiftUI(如何在SwiftUI中正确处理全景图大小调整)

转载 作者:bug小助手 更新时间:2023-10-25 11:17:59 26 4
gpt4 key购买 nike



I have a view which has numeric values in it, when they get to big to fit the frame, I would like the entire view to have all text in it to resize. I have added .scaledToFit() after the .frame(width: 150, height:150) , but it still seems to only resize the individual Text String that contains the numeric value, instead of the whole view. Here's view

我有一个有数值的视图,当它们变大以适应框架时,我希望整个视图中的所有文本都可以调整大小。我已经在.Frame(宽度:150,高度:150)之后添加了.scaledToFit(),但它似乎仍然只调整包含数值的单个文本字符串的大小,而不是整个视图。这里有一个观点


//
// ContentView.swift
// SampleMRE
//
// Created by Michael Rowe on 9/10/23.
//

import SwiftUI

var currencyFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.isLenient = true
formatter.numberStyle = .currency
return formatter
}()

struct ContentView: View {

var body: some View {
VStack {
HStack {
View1()
.frame(width: 150, height: 150)
.padding(5)
.background(Color.blue)
View1()
.frame(width: 150, height: 150)
.padding(5)
.background(Color.red)
}
}
.padding(.all, 40)
.background(Color.gray)
}
}

struct View1: View {
@State private var floatValue1: Float = 9_9999.0
@State private var floatValue2: Float = 999_999_999_999.99
@State private var floatValue3: Float = 999_999_999_999.99
var body: some View {
VStack {
HStack {
Text("Heading")
.bold()
.multilineTextAlignment(.center)
}
HStack {
Text("Value 1:")
.multilineTextAlignment(.leading)
Spacer()
Text(String(format: "%.0f", floatValue1))
.multilineTextAlignment(.leading)
}
.accessibilityElement(children: .combine)

SummaryLine(label: String(localized: "Value 2"), amount: currencyFormatter.string(from: NSNumber(value: floatValue2)) ?? "--")
SummaryLine(label: String(localized: "Value 3"), amount: currencyFormatter.string(from: NSNumber(value: floatValue3)) ?? "--")
}
.minimumScaleFactor(0.1)
.scaledToFill()
.frame(width: 150, height: 150)
.padding(20)
}
}

struct SummaryLine: View {
var label: String
var amount: String

var body: some View {
HStack {
Text(label)
.multilineTextAlignment(.trailing)
Spacer()
Text(amount)
.multilineTextAlignment(.leading)
}
.accessibilityElement(children: .combine)
}
}
#Preview {
ContentView()
}


更多回答
优秀答案推荐

The order of modifiers is very important and .scaledToFit must come before .frame.

修改器的顺序非常重要,.scaledToFit必须在.Frame之前。


Try it like this:

像这样试一试:


VStack {
// content as before
}
.minimumScaleFactor(0.1)
.scaledToFit()
.frame(width: 150, height: 150)
.padding(20)

If you still see small discrepancies then it may be because of the spacing on the HStack and VStack. Try using spacing: 0 on these and then use padding between the items.

如果您仍然看到微小的差异,则可能是因为HStack和VStack上的间距。尝试在这些项上使用间距:0,然后在这些项之间使用填充。


Screenshot from iPhone 14 Plus:

IPhone 14 Plus的屏幕截图:


Scaled


更多回答

I have tried that, and now the text overlaps between views, and goes outside of the frame. I've even tried adding .scaledToFit() both before and after the .frame. (note there are multiple views like above that group together in a larger view, i.e. a 300x150 with two views next to each other.

我试过了,现在文本在两个视图之间重叠,并且超出了框架。我甚至尝试在.Frame之前和之后添加.scaledToFit()。(请注意,在更大的视图中,有多个类似于上面的视图组合在一起,即300x150,其中两个视图彼此相邻。

It appears that .minimumScaleFactor(0.1).frame(width:150, height:150).scaledToFit() works on iPhone, but on iPad causes the size difference between items. And moving .scaledtoFit() before .frame causes issues on both.

似乎.minumScaleFactor(0.1).Frame(宽度:150,高度:150).scaledToFit()可以在iPhone上运行,但在iPad上会导致项目之间的大小不同。将.scaledtoFit()移到.Frame之前会导致两者都出现问题。

I hope you saw my additional note about stack spacing at the bottom (I added it in an edit). Otherwise, I would be happy to investigate further if you could kindly fix the MRE so that it compiles and runs as a standalone example.

我希望你看到我关于底部堆栈间距的补充说明(我在编辑中添加了它)。否则,我将很乐意进一步调查,如果您可以友好地修复MRE,使它作为一个独立的例子编译和运行。

I added colors to make it more obvious - will also try updating to try the spacing: 0

我添加了颜色以使其更明显-我还会尝试更新以尝试间距:0

Thanks for updating the MRE. It works for me when I change the modifiers on the VStack to the ones I provided (screenshot added to answer). Tried on iPhone 14 Plus and iPad mini 6G (iOS 16.4). I didn't even need to set spacing to 0 on the stacks, although I think there may be small discrepancies as I anticipated, so setting spacing to 0 may help to fix them. If you try it on a very small device then it is conceivable that the minimum scaling factor may need to be made smaller. Note that you are using scaledToFill in your MRE, very important to replace that line!

感谢您更新MRE。当我将VStack上的修改器更改为我提供的修改器(在答案中添加了屏幕截图)时,它对我有效。在iPhone 14 Plus和iPad mini 6G(iOS 16.4)上试用。我甚至不需要在堆栈上将间距设置为0,尽管我认为可能会像我预期的那样存在微小的差异,因此将间距设置为0可能有助于修复它们。如果你在一个非常小的设备上尝试,那么可以想象最小比例因数可能需要变得更小。请注意,您在MRE中使用了scaledToFill,替换该行非常重要!

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