简体   繁体   English

在 UIStackView 中更改 UIImageView 的高度优先级

[英]Change height priority of UIImageView inside UIStackView

I have an UIImageView inside UIView which is inside an horizontal UIStackView (with.fillEqually option).我在UIImageView UIView它位于水平UIStackView (with.fillEqually 选项)内。 Horizontal UIStackView has two views always.水平UIStackView总是有两个视图。 Horizontal UIStackView gets its height from the Its' highest sub UIView .水平UIStackView的高度来自其最高子UIView

What I want to achieve: In the example screenshot, UIImageView has a bigger size when you consider with the multiline views because of its image size.我想要实现的目标:在示例屏幕截图中,UIImageView 由于其图像大小而在考虑多行视图时具有更大的大小。 However, I want that image view shrinks to the size that fits left (multiline label) view.但是,我希望图像视图缩小到适合左侧(多行标签)视图的大小。 This is shown in the second screenshot.这显示在第二个屏幕截图中。 Image should has a height of which I show via red oval line on the right side.图像应该具有我通过右侧的红色椭圆线显示的高度。

UIImageView with label Class: UIImageView 与 label Class:

@IBDesignable class LabelWithImage: UIView {

var topMargin: CGFloat = 0.0
var verticalSpacing: CGFloat = 10.0
var bottomMargin: CGFloat = 0.0

@IBInspectable var labelText: String = "" { didSet { updateView() } }
@IBInspectable var image: UIImage = UIImage(named: "harcamagrafik")! { didSet { updateView() } }

fileprivate var label: UILabel!
fileprivate var imageView: UIImageView!

override init(frame: CGRect) {
    super.init(frame: frame)
    setUpView()
}

required public init?(coder: NSCoder) {
    super.init(coder:coder)
    setUpView()
}

override func prepareForInterfaceBuilder() {
    super.prepareForInterfaceBuilder()
    setUpView()
}

func setUpView() {

    label = UILabel()
    label.font = UIFont.init(name: "Metropolis-Medium", size: 12)
    label.numberOfLines = 1
    label.lineBreakMode = NSLineBreakMode.byWordWrapping
    label.textColor = UIColor.init(rgb: 0x9B9B9B)

    imageView = UIImageView(image: image)
    imageView.contentMode = .scaleAspectFill
    imageView.clipsToBounds = true

    addSubview(label)
    addSubview(imageView)

    label .translatesAutoresizingMaskIntoConstraints = false
    imageView.translatesAutoresizingMaskIntoConstraints = false
    setContentHuggingPriority(.init(rawValue: 1000.0), for: .vertical)
    setContentCompressionResistancePriority(.init(rawValue: 10.0), for: .vertical)

    label.leftAnchor.constraint(equalTo: leftAnchor, constant: 0.0).isActive = true
   // imageView.leftAnchor.constraint(equalTo: leftAnchor, constant: 0.0).isActive = true

    label.rightAnchor.constraint(equalTo: rightAnchor, constant: 0.0).isActive = true
   // imageView.rightAnchor.constraint(equalTo: rightAnchor, constant: 0.0).isActive = true

    imageView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
    label.topAnchor.constraint(equalTo: topAnchor, constant: topMargin).isActive = true
    imageView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: verticalSpacing).isActive = true
    bottomAnchor.constraint(equalTo: imageView.bottomAnchor, constant: bottomMargin).isActive = true

    // call common "refresh" func
    updateView()
}

func updateView() {
    label.text = labelText
    accessibilityIdentifier = labelText
    imageView.image = image
    label.sizeToFit()
}
}

Horizontal StackView:水平堆栈视图:

private let topHorizontalStackView: UIStackView = {
let s = UIStackView()
s.distribution = .fillEqually
s.spacing = 10
s.axis = .horizontal
s.alignment = .fill
s.translatesAutoresizingMaskIntoConstraints = false
return s
}()

EDIT I tried to set a height constraint to my LabelWithImage view.编辑我试图为我的 LabelWithImage 视图设置高度约束。 Now, It looks ok but when I open the debugger, It says that height is ambiguous for the view.现在,它看起来不错,但是当我打开调试器时,它说视图的高度不明确。

leftView.layoutIfNeeded() //It is the view with multilineLabel.
view.heightAnchor.constraint(equalToConstant: leftView.bounds.height).isActive = true

示例视图

What I want shown in screenshot:我想在屏幕截图中显示:

SS2

EDIT 2 After I give priority to label as @Don said and set compression to low on image view, I got below result.编辑 2正如@Don所说,我优先考虑 label 并在图像视图中将压缩设置为低,我得到了以下结果。 Below code is still active by the way顺便说一下,下面的代码仍然有效

leftView.layoutIfNeeded() //It is the view with multilineLabel. view.heightAnchor.constraint(equalToConstant: leftView.bounds.height).isActive = true

If I give specific constant to labelWithImageView as (40 is UIStackView top and bottom insets);如果我将特定常量赋予 labelWithImageView 为(40 是 UIStackView 顶部和底部插图);

view.heightAnchor.constraint(equalToConstant: leftView.bounds.height - 40).isActive = true

Views inside top vertical UIStackView shows ambitious layout.顶部垂直 UIStackView 内的视图显示了雄心勃勃的布局。 (You can't see that views inside screenshots because I guess those are not related.) (您无法在屏幕截图中看到该视图,因为我猜这些视图不相关。) ss3

Well, it just looks like your constraints are wrong, here I've made an example with screen attached so you can recreate my constraints好吧,看起来你的约束是错误的,这里我做了一个附加屏幕的例子,所以你可以重新创建我的约束

Here's an explanation: in left view set your own constraints to fill the view (check for ambiguity since right view is supposed to get the size from the left one), set all labels inside it with vertical content hugging priority to 1000这是一个解释:在左视图中设置您自己的约束来填充视图(检查歧义,因为右视图应该从左视图获取大小),将其中的所有标签设置为垂直内容拥抱优先级为 1000

on right one you simply need to set the title vertical content hugging priority to 1000 (+ basic needed constraints like leading, trailing and top to fill the view) and viola, image will morph into whatever size needed to match left view's size.在右侧,您只需将标题垂直内容拥抱优先级设置为 1000(+ 基本所需的约束,如前导、尾随和顶部填充视图)和中提琴,图像将变形为匹配左视图大小所需的任何大小。

在此处输入图像描述

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

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