【问题标题】:Resize UIImageView by changing frame通过更改框架调整 UIImageView 的大小
【发布时间】:2018-02-06 11:26:03
【问题描述】:

我在情节提要中添加了一个带有默认图像的 UIImageView。在代码中,我在运行时更新图像并调整框架。

mainImageView.sd_setImage(with: NSURL(string: urlString) as URL?, completed: {
        (image, error, cacheType, url) in
        self.setPoints()
    })

public func setPoints() {
    mainImageView.frame = CGRect(x:x, y:0, width:(mainImageView.image?.size.width)!/1.7, height:(mainImageView.image?.size.height)!/1.7)
    mainImageView.contentMode = .scaleAspectFill
    mainImageView.setNeedsDisplay()
    mainImageView.reloadInputViews()
    mainImageView.updateConstraintsIfNeeded()
}

我阅读了有关堆栈溢出的各种帖子,并尝试使用上述方法调整 imageivew 的大小,但没有运气。有没有其他方法可以调整 imageview 的大小?

【问题讨论】:

  • mainImageView 通过自动布局添加?如果是,那么您应该采取高度和宽度限制并进行管理。
  • mainImageView.translatesAutoresizingMaskIntoConstraints = YES;您可以将此代码放在您的 -viewDidLoad 方法中,或者放在 mainImageView.sd_setImage(with:...

标签: ios iphone swift uiimageview frame


【解决方案1】:

您应该禁用预生成的约束。最简单的方法是使用自动调整掩码而不是约束来动画视图。

mainImageView.translatesAutoresizingMaskIntoConstraints = true

您可以将此代码放入您的 -viewDidLoad 方法或在 mainImageView.sd_setImage(with:... 上方

最后但并非最不重要的是,您需要在块内调用 layoutIfNeeded。

self.view.layoutIfNeeded()

【讨论】:

    【解决方案2】:

    您似乎正在使用自动布局来调整图像视图的大小,这可以解释为什么手动设置框架没有任何作用。您需要修改决定图像视图大小的约束,然后调用mainImageView.layoutIfNeeded()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多