Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SwiftUI-如何围绕缩放的NSImage绘制边框_Swift_Cocoa_Swiftui - Fatal编程技术网

SwiftUI-如何围绕缩放的NSImage绘制边框

SwiftUI-如何围绕缩放的NSImage绘制边框,swift,cocoa,swiftui,Swift,Cocoa,Swiftui,我有一个缩放到当前窗口宽度/高度的图像。我需要在图像周围画一个边框。我已经尝试了下面的代码,但是,它并没有在图像周围画出一个精确的边框(见附件中的截图)。我错过了什么 import SwiftUI struct RectangleView: View { var body: some View { GeometryReader { geo in ZStack { Image("front")

我有一个缩放到当前窗口宽度/高度的图像。我需要在图像周围画一个边框。我已经尝试了下面的代码,但是,它并没有在图像周围画出一个精确的边框(见附件中的截图)。我错过了什么

import SwiftUI

struct RectangleView: View {
    var body: some View {
        GeometryReader { geo in
            ZStack {
                Image("front")
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 400, height: geo.size.height)
                    .clipShape(
                        RoundedRectangle(cornerRadius: 30)
                    )
                    .overlay(
                        RoundedRectangle(cornerRadius: 30)
                            .stroke(lineWidth: 10).foregroundColor(Color.green)
                    )
            }
            .padding()
            .position(x:geo.frame(in:.global).midX,y:geo.frame(in:.global).midY)
        }
    }
}
下面是我的视图控制器中的代码

import Cocoa
import SwiftUI

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let rectangleView = RectangleView()
        let contentView = NSHostingController(rootView: rectangleView)
        let rectView = contentView.view
        rectView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(rectView)

        NSLayoutConstraint.activate([
            rectView.leadingAnchor.constraint(equalTo: rectView.superview!.leadingAnchor),
            rectView.trailingAnchor.constraint(equalTo: rectView.superview!.trailingAnchor),
            rectView.topAnchor.constraint(equalTo: rectView.superview!.topAnchor),
            rectView.bottomAnchor.constraint(equalTo: rectView.superview!.bottomAnchor)
        ])
    }

}


您已经为图像
.frame(宽度:400,高度:geo.size.height)提供了一个静态帧。

因此,您需要具体说明纵横比。
使用.fill将
.aspectRatio(contentMode:.fit)
更改为
.aspectRatio(contentMode:.fill)

。我需要图像始终保持在给定宽度内的纵横比。