【问题标题】:ios: UIImageView with Aspect Fill not fitting the constraintios:带有Aspect Fill的UIImageView不适合约束
【发布时间】:2017-07-31 02:35:32
【问题描述】:

我有以下设计。使用故事板 我尝试了多种方法,但所有的努力都是徒劳的。

1 - 视图控制器的主要默认视图 2 - 包含图像视图和 2 个等宽按钮的视图 3 - 带有 Aspect Fill 的图像视图(需要根据 iPhone 尺寸占用尽可能多的空间)

我做对了除了 1 个约束。包含 2 个按钮的 ImageView 和 View 之间的约束。 (对按钮使用 Stack 视图)

在某些屏幕尺寸中,具有纵横比填充的图像视图与堆栈视图重叠。 Aspect Fit 留下大量空白。

Image View 需要根据尺寸为所有 iPhone 缩放! (这似乎可以通过 Aspect Fill 解决,而不是 aspect fit) 如何解决 ImageView 缩放时保持 Image + 2 按钮垂直居中(矩形 2 垂直和水平居中)的问题?

编辑:我也尝试过“Clip to Bound”,但它会在侧面剪辑图像。我想知道完整的设置,每个视图应该做什么或者每个视图应该如何组织以获得效果?

【问题讨论】:

    标签: ios iphone swift interface-builder uistoryboard


    【解决方案1】:

    也许您的约束设置正确。尝试为 imageview 设置“Clip To Bounds”标志。

    除此之外,您可以向图像视图添加一个与它包含的图像的纵横比相匹配的纵横比约束。

    更新

    这是您以编程方式设置约束的方式(例如在viewDidLoad 中):

    let containerView: UIView = {
        let cv = UIView()
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.backgroundColor = UIColor.lightGrayColor()
        return cv
    }()
    let imageView: UIImageView = {
        let iv = UIImageView()
        iv.translatesAutoresizingMaskIntoConstraints = false
        iv.backgroundColor = UIColor.darkGrayColor()
        iv.image = UIImage(named: "Forest") // YOUR IMAGE HERE
        return iv
    }()
    let leftButton: UIButton = {
        let lb = UIButton(type: .System)
        lb.translatesAutoresizingMaskIntoConstraints = false
        lb.setTitle("Left Button", forState: .Normal)
        return lb
    }()
    let rightButton: UIButton = {
        let rb = UIButton(type: .System)
        rb.translatesAutoresizingMaskIntoConstraints = false
        rb.setTitle("Right Button", forState: .Normal)
        return rb
    }()
    let stackView: UIStackView = {
       let sv = UIStackView()
        sv.translatesAutoresizingMaskIntoConstraints = false
        sv.axis = .Horizontal
        sv.distribution = .FillEqually
        return sv
    }()
    
    stackView.addArrangedSubview(leftButton)
    stackView.addArrangedSubview(rightButton)
    
    view.addSubview(containerView)
    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[cv]-|", options: [], metrics: nil, views: ["cv": containerView]))
    containerView.centerYAnchor.constraintEqualToAnchor(view.centerYAnchor).active = true
    
    containerView.addSubview(imageView)
    containerView.addSubview(stackView)
    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[iv]-|", options: [], metrics: nil, views: ["iv": imageView]))
    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[iv]-[sv]-|", options: [.AlignAllLeading, .AlignAllTrailing], metrics: nil, views: ["iv": imageView, "sv": stackView]))
    
    let image = imageView.image!
    imageView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Width, relatedBy: .Equal, toItem: imageView, attribute: .Height, multiplier: image.size.width / image.size.height, constant: 0))
    

    结果如下:

    【讨论】:

    • 我在发布这个问题之前尝试过。它不起作用。它在侧面剪辑图像。我想知道完整的设置,每个视图应该做什么或者每个视图应该如何组织以获得效果?
    • 谢谢。我会用 Storyboard 试试这个然后回来。我不太熟悉约束代码,为了与现有代码保持一致,我会继续使用 Storyboard。不过感谢您的努力!
    • 这里是在故事板中设置的方法:github.com/andreslotta/SOSetup
    • 太棒了。正是我想要的。所以我面临的确切问题是我缺少图像的“Ration To”约束。由于图像的矩形纵横比,Aspect Fill 溢出,Aspect Fit 总是更小。谢谢。
    • 很高兴我能帮忙?
    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    相关资源
    最近更新 更多