【问题标题】:Swift programmatically create uiimageview full width of screen keeping height ratioSwift以编程方式创建uiimageview全宽屏幕保持高度比
【发布时间】:2019-06-15 12:55:59
【问题描述】:

我正在使用以下代码以编程方式创建 uiimageview。

var imageView : UIImageView
imageView  = UIImageView(frame:CGRectMake(10, 50, 100, 300));
imageView.image = UIImage(named:"image.jpg")
self.view.addSubview(imageView)

我遇到的问题是我希望 uiimageview 是屏幕的全宽,并且高度要调整到当前图像高度。我正在将不同大小的图像加载到这个 uiimageview 中。

如何做到这一点?

【问题讨论】:

    标签: swift uiimageview


    【解决方案1】:

    您可以通过几种方式来解决这个问题。我想填充图像的任一侧并使其居中。

        view.addSubview(imageView)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        imageView.widthAnchor.constraint(equalToConstant: view.frame.width - 80).isActive = true
        imageView.heightAnchor.constraint(equalToConstant: view.frame.width - 80).isActive = true
    

    【讨论】:

      【解决方案2】:

      您必须将 contentMode 设置为 .scaleAspectFit 。

      let imageView = UIImageView(frame: self.view.frame) 
      imageView.image = UIImage(named:"image.jpg") 
      imageView.contentMode = .scaleAspectFit 
      self.view.addSubview(imageView)
      

      【讨论】:

      • 这不会改变图像视图的高度。这会改变图像在图像视图中的显示方式。
      【解决方案3】:

      没有内置方法可以让UIImageView 根据image 自行调整大小。您必须通过更改框架的高度或在更新图像时设置新的纵横比约束来调整 imageView 的高度。

      UIImageView 上有一个 contentMode 设置 (.scaleAspectFit) 可以保持 imageView 中图像的纵横比,但它不会调整 imageView 本身的大小,而只是适合其中的图像添加根据需要在图像上方和下方或两侧进行填充以保持纵横比。

      更新框架:

      这是UIImageView 的扩展,当设置image 时,它会改变frame 的高度:

      extension UIImageView {
          func setImageAndUpdateFrameHeight(image: UIImage) {
              self.image = image
      
              if image.size.width > 0 {
                  self.frame.size.height = self.frame.size.width / image.size.width * image.size.height
              }
          }
      }
      

      使用自动布局时更新纵横比

      如果您使用的是自动布局,那么您希望在更新图像时设置一个新的 aspectRatio 约束。使用UIImageView 的这个子类来维护UIImageView 的aspectRatio 约束。您需要添加约束来放置图像并设置其宽度。

      class ResizingImageView: UIImageView {
          var aspectRatio: NSLayoutConstraint?
      
          func setImageAndUpdateAspectRatio(image: UIImage) {
              self.image = image
      
              aspectRatio?.isActive = false
              if image.size.width > 0 {
                  aspectRatio = self.heightAnchor.constraint(equalTo: self.widthAnchor, multiplier: image.size.height / image.size.width)
              }
              aspectRatio?.isActive = true
          }
      }
      

      注意事项:

      1. 如果在 Storyboard 中进行设置,请在 Identity Inspector 中拖出 UIImageView 并将其类更改为 ResizingImageView。
      2. 设置约束以放置 imageView 并确定其宽度(例如:为其父视图设置前导和尾随约束)。
      3. 给它一个高度约束,然后在Size Inspector中将该约束的优先级改为Low (250),这样apectRatio约束就可以控制imageView的高度。
      4. 将@IBOutlet 添加到您的图像视图:@IBOutlet var imageView: ResizingImageView!
      5. 添加图片时:imageView.setImageAndUpdateAspectRatio(image: newImage)

      【讨论】:

      • 这有帮助吗?你有什么问题吗?
      猜你喜欢
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 2016-01-17
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多