简体   繁体   English

按比例设置 UIImageView 的大小

[英]Setting the size of UIImageView proportionally

I have a question about how to set the size of UIImageView based on iPhone's display size dynamically.我有一个关于如何根据 iPhone 的显示大小动态设置 UIImageView 大小的问题。

If the size of a display of iPhone decreases, I want to make the size of UIImageView decreased proportionally.如果 iPhone 的显示屏尺寸减小,我想让 UIImageView 的尺寸按比例减小。

I'm setting lowerThumbImageView and upperThumbImageView like below for now.我现在正在设置如下所示的lowerThumbImageView 和 upperThumbImageView

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

    trackLayer.rangeSlider = self
        // contentsScale: CGFloat - The scale factor applied to the layer.
        // scale: CGFloat - The natural scale factor associated with the screen.
    trackLayer.contentsScale = UIScreen.main.scale
        // UIControl -> UIView -> layer
    layer.addSublayer(trackLayer)

    lowerThumbImageView.image = thumbImageA
    addSubview(lowerThumbImageView)

    upperThumbImageView.image = thumbImageB
    addSubview(upperThumbImageView)

  }


// 1
  private func updateLayerFrames() {
        // insetBy(dx:dy:) - Returns a rectangle that is smaller or larger
        //                   than the source rectangle, with the same center point.
    trackLayer.frame = bounds.insetBy(dx: 0.0, dy: bounds.height / 3)
    trackLayer.setNeedsDisplay()
    lowerThumbImageView.frame = CGRect(origin: thumbOriginForValue(lowerValue),
                                       size: thumbImageA.size)
    upperThumbImageView.frame = CGRect(origin: thumbOriginForValue(upperValue),
                                       size: thumbImageB.size)
    CATransaction.begin()
    CATransaction.setDisableActions(true)
    CATransaction.commit()

  }
  // 2
  func positionForValue(_ value: CGFloat) -> CGFloat {

    return bounds.width * value
  }
  // 3
  private func thumbOriginForValue(_ value: CGFloat) -> CGPoint {
    let x = positionForValue(value) - thumbImageA.size.width / 2.0
    return CGPoint(x: x, y: (bounds.height * 0.001 - thumbImageA.size.height) / 2.0)
  }

Could you give me your advice?你能给我你的建议吗?

Thanks for reading.谢谢阅读。

You can set size of UIImageView proportional to any other View.您可以将 UIImageView 的大小设置为与任何其他 View 成比例。 Here are the steps to do it using Interface Builder Autolayout Constraints.以下是使用 Interface Builder Autolayout Constraints 执行此操作的步骤。

  1. Go to Screen containing UIImageView on (Interface Builder/StoryBoard/XIB).转到包含 UIImageView 的屏幕(界面生成器/故事板/XIB)。
  2. Press control button + drag mouse from UIImageView to SuperView or main View, Select "Equal Widths" & "Equal Heights".按控制按钮+从 UIImageView 拖动鼠标到 SuperView 或主视图,选择“等宽”和“等高”。
  3. Now from the right side in SizeInspector, Edit "Proportional Width" constraint & set its Multiplier value to 0.8 if you want to set width of UIImageView 80% to SuperView.现在从 SizeInspector 的右侧,编辑“Proportional Width”约束并将其 Multiplier 值设置为 0.8,如果要将 UIImageView 的宽度设置为 80% 到 SuperView。
  4. Edit "Proportional Height" constraint & set its Multiplier value to 0.7 if you want to set width of UIImageView 70% to SuperView.如果要将 UIImageView 的宽度设置为 70% 到 SuperView,请编辑“Proportional Height”约束并将其 Multiplier 值设置为 0.7。

在此处输入图片说明 I've attached images for your ease.为了您的方便,我附上了图片。 在此处输入图片说明 在此处输入图片说明

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

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