如何解决SwiftUI 中的填充、偏移或位置
我正在尝试定位一个圆,使其中心位于像这样的矩形的顶部。圆的直径不必与矩形的宽度相同。
我想了解的是实现这一目标的最佳和最实用的方法是什么?在这种情况下,我应该使用填充、偏移或位置吗?它是否适用于不同的设备/屏幕尺寸?似乎可以使用它们中的任何一个,但哪个更理想?
这是我使用 padding
的结果。我可以增加底部填充来达到我想要的效果,但不确定这是否是最好的方法。
Rectangle()
.fill(Color.gray)
.frame(width: 85,height: 175)
.overlay(
Circle()
.fill(Color.black)
.frame(width: 120)
.padding(.bottom,50)
)
解决方法
对于形状,一个好的方法是像这样组合叠加和偏移:
// pass user input back
@IBAction func priceBtnTap(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
switch sender.tag {
case 1:
delegate?.passPrice(price: oneDollarPriceLabel.text!)
case 2:
delegate?.passPrice(price: twoDollarPriceLabel.text!)
case 3:
delegate?.passPrice(price: threeDollarPriceLabel.text!)
case 4:
delegate?.passPrice(price: fourDollarPriceLabel.text!)
case 5:
delegate?.passPrice(price: fiveDollarPriceLabel.text!)
default:
print("Problem in priceBtnTap()")
}
func passPrice(price: String) {
priceTarget = price
print(priceTarget)
}
var priceTarget = "10"
//MARK: - Fetch stockDta
func fetchData() {
guard let urlString = URL(string: "https://financialmodelingprep.com/api/v3/stock-screener?sector=technology&priceLowerThan=\(priceTarget)&exchange=NYSE,NASDAQ,AMEX&limit=100&apikey=\(Api.key)") else {
fatalError("Wrong endpoint")
}
let dataTask = session.dataTask(with: urlString) { [self] (data,_,error) in
if error != nil {
print("opps,we have a problem")
return
}
if let payload = data {
guard let stockInfo = try? JSONDecoder().decode([StockData].self,from: payload) else {
print("issue decoding data")
return
}
self.stockArray.append(contentsOf: stockInfo)
//MARK: - iterate over the stock data and save all the stock symbols in a new array
for stockSymbol in stockInfo {
self.stockSymbolArray.append(stockSymbol.symbol!)
}
//MARK: - Call the fetchStockChangeData() and pass the stockSymbolArray as a paramater of the function
fetchStockChangeData(stock: stockSymbolArray)
}
DispatchQueue.main.async {
self.tabelview.reloadData()
}
}
dataTask.resume()
}
,
这是一种可能的方法 - 仅使用对齐而不使用硬编码偏移/位置。
使用 Xcode 12.4 / iOS 14.4 准备的演示
var body: some View {
Rectangle()
.fill(Color.gray)
.frame(width: 85,height: 175)
.overlay(
Circle()
.stroke(Color.black,style: StrokeStyle(
lineWidth: 2,lineCap: .round,lineJoin: .miter,miterLimit: 0,dash: [5,10],dashPhase: 0
))
.frame(width: 85,height: 85)
.alignmentGuide(.top) { $0[VerticalAlignment.center] } // << this !!,alignment: .top) // and this !!
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。