微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SwiftUI 中的填充、偏移或位置

如何解决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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?