SwiftUI 中的填充、偏移或位置

问题描述

我正在尝试定位一个圆,使其中心位于像这样的矩形的顶部。圆的直径不必与矩形的宽度相同。

enter image description here

我想了解的是实现这一目标的最佳和最实用的方法是什么?在这种情况下,我应该使用填充、偏移或位置吗?它是否适用于不同的设备/屏幕尺寸?似乎可以使用它们中的任何一个,但哪个更理想?

这是我使用 padding 的结果。我可以增加底部填充来达到我想要的效果,但不确定这是否是最好的方法

Rectangle()
    .fill(Color.gray)
    .frame(width: 85,height: 175)
    .overlay(
        Circle()
            .fill(Color.black)
            .frame(width: 120)
            .padding(.bottom,50)
    )

enter image description here

解决方法

对于形状,一个好的方法是像这样组合叠加和偏移:

// 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()
        
    }
    



enter image description here

,

这是一种可能的方法 - 仅使用对齐而不使用硬编码偏移/位置。

使用 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 !!
}

demo

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...