简体   繁体   English

Swift 如何将UIStackView放置在特定的角落 ImageView

[英]Swift How to Place UIStackView in a Specific Corner of ImageView

So basically I have an ImageView and a HorizontalStackView ( the one circled in red ).所以基本上我有一个ImageView和一个HorizontalStackView ntalStackView(红色圆圈中的那个)。 I am trying to overlay the stackview layout in the TopRight Corner of the picture once the user has clicked on the button.用户单击按钮后,我试图在图片的右上角覆盖堆栈视图布局。 Needless to say that I do not have control over the picture being loaded in ImageView since it is being fetched from the inte.net.不用说,我无法控制 ImageView 中加载的图片,因为它是从 inte.net 获取的。 I read on Apple doc that.contentMode will get the result I am looking.我在 Apple 文档上读到。contentMode 将得到我正在寻找的结果。 Thus, this is my code sample因此,这是我的代码示例

 case .UpRight:
        repostLayout.contentMode = .topRight
        imageView.addSubview(repostLayout)

As you can expect there is no change when running the app.如您所料,运行应用程序时没有任何变化。 Can someone point me in the right direction how I would be able to get it done?有人可以指出我如何完成它的正确方向吗?

Thanks in advance...提前致谢...

If I understood what you are trying to achieve, why not have a wrapping UIView contain both the image and stackView as subviews and simply constraint the image to the view's dimensions and the stackview to the top right corner?如果我理解你想要实现的目标,为什么不让包装 UIView 包含图像和 stackView 作为子视图,并简单地将图像限制在视图的尺寸和 stackview 的右上角?

So as an example:举个例子:

class MyViewController: UIViewController {
    let wrapperView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    //Your existing stack
    private let stackView: UIStackView = {
        let stack = UIStackView()
        stack.axis = .horizontal
        return stack
    }()
    
    //Your existing imageView
    private let imageView: UIImageView = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //Shouldn't necessarily be in viewDidLoad, but just for example:
        view.addSubview(wrapperView)
        //Set wrapperView's constraints to where the image currently is
        
        imageView.translatesAutoresizingMaskIntoConstraints = false
        stackView.translatesAutoresizingMaskIntoConstraints = false
        
        wrapperView.addSubview(imageView)
        wrapperView.addSubview(stackView)
        imageView
            .topAnchor
            .constraint(equalTo: wrapperView.topAnchor)
            .isActive = true
        imageView
            .bottomAnchor
            .constraint(equalTo: wrapperView.bottomAnchor)
            .isActive = true
        imageView
            .leadingAnchor
            .constraint(equalTo: wrapperView.leadingAnchor)
            .isActive = true
        imageView
            .trailingAnchor
            .constraint(equalTo: wrapperView.trailingAnchor)
            .isActive = true
        
        stackView
            .topAnchor
            .constraint(equalTo: wrapperView.topAnchor)
            .isActive = true
        stackView
            .trailingAnchor
            .constraint(equalTo: wrapperView.trailingAnchor)
            .isActive = true
        
        //Make sure your stack view is not too wide or too tall for the wrapper
        //Either by using constraints or by any other method.
    }
}

The same can easily be achieved via Storyboards.通过故事板可以轻松实现相同的目的。 If you get stuck i'll post the same for storyboards.如果您遇到困难,我会为情节提要发布相同的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM